1

I am trying to send some props to my another component but it is giving this error: enter image description here

enter image description here enter image description here

3
  • 1
    Please give a minimal reproducible example, don't post pictures of text. Commented Sep 12, 2022 at 17:32
  • Relevant code and error messages need to be included in your question as text, not as pictures of text. Just linking to screen shots makes it more difficult for people to help you. To learn more about this community and how we can help you, please start with the tour and read How to Ask and its linked resources. Commented Sep 12, 2022 at 17:34
  • Additionally, the error implies that the <AddToCart> component is not expecting this book prop. We know nothing of this component. Commented Sep 12, 2022 at 17:35

1 Answer 1

1

I'm assuming you're not declaring AddToCart's props properly (no pun intended).

Your code should look something like this:

type Book = {
  title: string;
  author: string;
  description: string;
};

type CartProps = {
  book: Book;
};

//    btw: components (and their names) should represent *things* not actions
const AddToCart = ({ book }: CartProps) => {
//    ^^^^^^^^^
  return (
    <div>
      <h1>{book.title}</h1>
      <h2>{book.author}</h2>
      <p>{book.description}</p>
    </div>
  );
};

export default function App() {
  const book = {
    title: "Some Title",
    author: "John Smith",
    description: "Book description."
  };

  return <AddToCart book={book} />;
}

Here's a working codesandbox.


Aside

Reserve “verb” names for methods/functions, not components. Component names should represent entities (for the most part, ex: Cart), not actions (ex: AddToCart).

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

3 Comments

We can use directly write this { book }: Book Why write this { book }: CartProps
@ZebraCoder If I'm understanding, you're asking why the object containing book is set to CartProps and not Book? Because it is not true that { book } is of type Book; it is an Object containing a Book object. The bracketed object "book" is not what CartProps is type controlling; book is just one of many props that AddToCart can potentially consume (ex: { book, ASIN, timestamp }). This example was written assuming a general cart system; as in one that will include more keys in CartProps.
@ZebraCoder you can try it out in the answer's codesandbox above for clarification. If you try to set it to Book it will throw typing errors for this reason.

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.