Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typing an array of objects as props in React

I have a functional component like

function ItemList({ items }: ItemProps[]) {
  return <p>items[0].name</p>
}

and I’m creating it like:

<ItemList items={items} />

items is an array of objects like [{name: 'a' id:0}, {name: 'b' id:1}].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Everything is working, but in ItemList.jsx TypeScript is telling me Property 'items' does not exist on type 'ItemProps[]'

>Solution :

function ItemList({ items }: ItemProps[]) {

This type doesn’t mean that items is an array, it means the props object is an array. You need:

function ItemList({ items }: { items: ItemProps[] }) {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading