Sub topic 6
Why data flows down from parent to child
This chapter assumes you can write a function component, read JSX, and pass props to a child. It explains the rule that connects the static version of the product table to the interactive version you build next.
The rule: props travel one direction
In React, a parent passes data to a child through props. The child reads those props. It does not change them, and it cannot send data back up through them. This is called one-way data flow, or a top-down data flow.
Look at the static product table. ProductTable receives products and hands each item to a ProductRow:
rows.push(<ProductRow product={product} key={product.name} />);ProductRow can read product.price and product.stocked. It has no way to edit the array that ProductTable owns. Information moves from the top of the tree toward the leaves.
Why React works this way
One-way flow makes a UI predictable. If a value on screen looks wrong, you trace it upward. Each component got that value from exactly one parent, so the search has a short path.
It also makes rendering a plain function of its inputs. Given the same props, ProductRow always returns the same markup. You can read the component and know what it shows without checking what other components have done.
Compare this with a design where any child can write to shared data. A row could edit a product, and a header could read the edited value. Then the screen depends on the order of those edits, and bugs appear far from their cause.
Props are read-only
A component must never modify its own props. This code breaks the rule:
function ProductRow({ product }) {
product.name = product.name.toUpperCase(); // Wrong: mutates a prop
return (
<tr>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
);
}The change alters an object that belongs to the parent. Other components that hold the same object now see the new name, and React has no signal to re-render them. Compute a new value instead:
function ProductRow({ product }) {
const name = product.name.toUpperCase();
return (
<tr>
<td>{name}</td>
<td>{product.price}</td>
</tr>
);
}What this means for the product table
The rule gives you a design constraint. Data that several components need must live in a shared parent, because siblings cannot pass data to each other. SearchBar and ProductTable are siblings. Both will need filterText and inStockOnly. Neither can hand the values to the other, so the values must sit in their common parent, FilterableProductTable. That parent passes them down.
This leaves one problem. When a user types in the search box, the new text starts in SearchBar, which is a child. Props only flow down, so the child needs another way to tell the parent about the change. Later chapters solve this with callback props, an approach called inverse data flow. For now, remember that it does not break the rule. Data still flows down. The child only calls a function it received from above.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A child changes but its sibling does not update | The child copied or mutated data instead of sharing it through a parent | Move the data to the common parent and pass it down |
| Editing a prop has no visible effect, or has odd effects | The component mutates a prop | Derive a new value without changing the original |
| Two components show different values for the same thing | Each keeps its own copy | Keep one source of truth and pass it down |
Exercise
In the product table, SearchBar and ProductTable both need the current search text. Why can't SearchBar simply pass the text to ProductTable directly, and where must the text live instead?
Show answer
Props flow only from parent to child, and SearchBar and ProductTable are siblings, so neither can send data to the other. The text must live in their closest common parent, FilterableProductTable, which passes it down to both as a prop.
Exercise
A teammate writes product.stocked = false inside ProductRow to hide an item. Name the rule it breaks and one problem it causes.
Show answer
It breaks the rule that props are read-only. The change mutates an object owned by the parent, so other components holding that object see the change without React re-rendering them, and the screen can fall out of sync with the data.