Middle+
If the parent component passes a new array as props, will you see the new data on the screen or the old? Why?
sobes.tech AI
Answer from AI
If the parent component passes a new array via props, the child component will display the new data. This is because React compares props by reference. When a new array is passed, the reference changes, so React understands that the props have been updated and re-renders the component with the new values.
Example:
function Parent() {
const [items, setItems] = React.useState([1, 2, 3]);
// Create a new array on update
const updateItems = () => setItems([4, 5, 6]);
return <Child data={items} />;
}
function Child({ data }) {
return <div>{data.join(", ")}</div>;
}
Calling updateItems in Parent will display the new data in Child.