Performance: Parent-child data flow
This concept affects how efficiently data updates propagate through components, impacting interaction responsiveness and rendering speed.
function Parent() { const [count, setCount] = React.useState(0); const data = React.useMemo(() => ({ count }), [count]); return <Child data={data} />; } function Child({ data }) { console.log('Child rendered'); return <div>{data.count}</div>; }
function Parent() { const [count, setCount] = React.useState(0); return <Child data={{ count, timestamp: Date.now() }} />; } function Child({ data }) { console.log('Child rendered'); return <div>{data.count}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing new object literals as props | Child re-renders every parent update | Triggers reflow per child render | Paints child content each time | [X] Bad |
| Memoizing props with useMemo | Child re-renders only on data change | Single reflow per actual update | Paints only changed content | [OK] Good |
title with value "Hello" from a parent to a child component Child?<Child /> component?function Parent() {
const message = "Hi there!";
return <Child text={message} />;
}
function Child(props) {
return <h1>{props.text}</h1>;
}function Parent() {
const data = "Hello";
return <Child data=data />;
}
function Child(props) {
return <div>{props.data}</div>;
}data=data without braces, so it passes string "data" instead of variable value.function Parent() {
const items = ["Apple", "Banana", "Cherry"];
return <Child items={items} />;
}
function Child(props) {
return (
<ul>
{props.items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}Pass the test and this pattern moves unknown