3

Simple case; I'm rendering a list of 'Reviews'. These are provided using the following Proptype:

export interface Props {
    title: string;
    name: string;
    reviewdesc: string;
    rating: number;
}

Mapping through the results in the parent component:

{reviews.map((review: Props) => {
    return <Review data={review} />;
})}

And using the same Proptypes in the child component:

const Review = (data: Props) => { ...

It is giving me this error:

Type '{ data: Props; }' is not assignable to type 'IntrinsicAttributes & Props'.
  Property 'data' does not exist on type 'IntrinsicAttributes & Props'.

It feels like I'm forgetting a little thing. I thought I should catch the Props like {data} in the child component, but it gives:

Property 'data' does not exist on type 'Props'.

1 Answer 1

7

You are passing props incorrectly. Use,

<Review { ...review } />

... is called the spread operator and it "spreads" the properties of your object into props for that element.

Sign up to request clarification or add additional context in comments.

1 Comment

Never knew this syntax. Thank you so much!

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.