0

I am trying to send this props to a child component from parent:

{
    "days": 7,
    "hours": 20,
    "minutes": 51,
    "seconds": 7
}

the parent component calling the child looks like this:

const FunctionSolvingTime=()=>{
//returns always object as above
return 
{
    "days": 7,
    "hours": 20,
    "minutes": 51,
    "seconds": 7
}
}

const ParentComponent=()=>{
const [timeLeft, setTime]=useState(FunctionSolvingTime())



//some logic here

return(
<childComponent timeLeft={timeLeft}/>
)

the child component consuming the time object is:


type TTimeLeft = {
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
};


const childComponent = (timeLeft: TTimeLeft) => {

//consume the data here
//{timeLeft.days},{timeLeft.hours}...
}

the error I am getting looks like


Type '{ timeLeft: { days: number; hours: number; minutes: number; seconds: number; }; }' is not assignable to type 'IntrinsicAttributes & TTimeLeft'.
  Property 'timeLeft' does not exist on type 'IntrinsicAttributes & TTimeLeft'. 

any idea what am I doing wrongly here?

thanks

1
  • 1
    Try specify type React.FC<TTimeLeft> on childComponent. Plus, component props are passed as an object. So the function header would be like const childComponent: React.FC<TTimeLeft> = ({ timeLeft }) => ... Commented Apr 21, 2022 at 14:30

1 Answer 1

2

Props are passed as object

const childComponent = ({ timeLeft }: { timeLeft: TTimeLeft }) => {

//consume the data here
//{timeLeft.days},{timeLeft.hours}...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.