Passing the full object into the component?
{list.map(
(item => (
<Child item={item} key={item.id} />
)
)}
Or using destructuring?
{list.map(
(({id, prop1, prop2, prop3}) => (
<Child key={id} prop1={prop1} prop2={prop2} prop3={prop3} />
)
)}
Maybe TypeScript can change or add the new approach somehow?
2
Answers
There is no best option, as it depends on your use case. But there are subtle differences:
In second case,
React.memo
, as react compares props and decide whether it should re-render. Passing an object (which would probably be created fresh in every render) would defeat the purpose and you would have to write extra logic to fix it.In first case,
Child
can mutateitem
by mistake. You probably do not want that. This does not look like great abstraction.item
changes together and then extracting individual prop likeprop1
,prop2
,prop3
do not make sense.I agree with Tushar Shai, that it depends on your use case.
Also consider that when you pass the full object
item={item}
, the child component will not rerender when only a property, say prop1, of item has changed, whereas in your second proposition the Child-component would rerender and display the changed property.