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. You also need to know the product table components from the static version: ProductCategoryRow, ProductRow, and ProductTable. The chapter explains the rule that shaped that static version. Data moves in one direction, from a parent component to its children. It also covers why React enforces this and what breaks when you work against it.
The rule
In React, a parent gives data to a child through props. The child can read those props and render from them. It cannot change them, and it cannot reach up and replace what the parent holds. This is called one-way data flow, or a top-down data flow.
Every component in a React tree has one parent, except the root. So for any value on screen, you can follow the chain of props upward until you reach the component that owns it. That component is the only place the value can be changed.
The diagram already includes filterText and inStockOnly. Those are state values, and later chapters cover them. For now, treat them as ordinary values the top component hands down.
The static version already follows the rule
Your static version has no state, so data only has one possible route. The array enters at the top and each component passes the relevant slice down.
const PRODUCTS = [
{ category: 'Fruits', price: '$1', stocked: true, name: 'Apple' },
{ category: 'Fruits', price: '$1', stocked: true, name: 'Dragonfruit' },
{ category: 'Fruits', price: '$2', stocked: false, name: 'Passionfruit' },
{ category: 'Vegetables', price: '$2', stocked: true, name: 'Spinach' },
{ category: 'Vegetables', price: '$4', stocked: false, name: 'Pumpkin' },
{ category: 'Vegetables', price: '$1', stocked: true, name: 'Peas' }
];
function FilterableProductTable({ products }) {
return (
<div>
<SearchBar />
<ProductTable products={products} />
</div>
);
}
export default function App() {
return <FilterableProductTable products={PRODUCTS} />;
}This snippet assumes SearchBar and ProductTable are defined as in the earlier chapters. Follow products through the tree:
Appsupplies the whole array.FilterableProductTablereceives it and forwards it toProductTable.ProductTableloops over it. For each product it renders aProductRowand passesproductdown. When the category changes, it renders aProductCategoryRowand passescategorydown.
Each component sees only what its parent decided to give it. ProductRow knows nothing about the other products, the category, or the array. It receives one product object and renders it.
Props are read-only
React treats a component as a function of its inputs. Given the same props, it should return the same JSX. If a child could change its props, the same call would depend on what happened earlier, and the output would stop being predictable.
React enforces part of this. In development, it freezes the props object. Assigning to a prop throws an error in strict mode, which includes ES modules:
function ProductRow({ product }) {
// Wrong: this tries to change data that belongs to the parent.
product.price = '$0';
return (
<tr>
<td>{product.name}</td>
<td>{product.price}</td>
</tr>
);
}This example hides a subtler problem. React freezes the props object itself (the { product } wrapper), not the objects inside it. product is a plain object shared with the parent, so product.price = '$0' succeeds. The failure is silent:
- The change happens during render, which should have no side effects.
- The shared
PRODUCTSentry is now modified for every other component that reads it. - React is not told anything changed, so nothing re-renders to reflect it. The screen and the data can disagree.
- In Strict Mode, React renders components twice in development, which makes impure renders easier to spot.
If a child needs a different version of the data, it computes a new value instead of editing the old one:
function ProductRow({ product }) {
const displayPrice = product.stocked ? product.price : 'Out of stock';
return (
<tr>
<td>{product.name}</td>
<td>{displayPrice}</td>
</tr>
);
}displayPrice is a new local value built from the props. The original product is untouched.
Why React works this way
A value has one source
When data flows only downward, each piece of data has one owner. If the price on screen is wrong, you look at the component that renders it, then the one that passed it, and continue upward until you find where the value came from. The search path is a straight line.
If children could write to shared data, any component could be the cause of any change, and debugging means searching the whole tree.
Rendering is a function of data
React's model is that the UI is the result of calling your components with the current props and state. When data changes, React calls the affected components again and compares the new output with the old one. This works because information has one direction. A re-render starts at the component whose data changed and proceeds downward. React does not have to ask whether a child secretly changed something its parent depends on.
By default, when a parent re-renders, its children re-render too, even if their props are identical. This is usually fast enough, and tools like React.memo can skip the work when you measure a real problem. The point here is that re-rendering follows the same direction as the data.
Components stay reusable
ProductRow works anywhere you can give it a product. It does not reach into a global store or call up to a specific parent. You can test it by calling it with a few sample objects, and you can reuse it in another table.
What one-way flow means for siblings
Siblings cannot pass data directly to each other. SearchBar and ProductTable are siblings. Later, SearchBar will hold the text the user types, and ProductTable will need that text to filter rows. The data cannot go sideways.
The only route goes through their closest common parent: the parent holds the value, then passes it to both. The sibling relationship is the reason the next chapters spend time on where state should live.
| Direction | Mechanism | Example |
|---|---|---|
| Parent to child | Props | `<ProductRow product={product} />` |
| Child to parent | A function passed down as a prop and called by the child | `onFilterTextChange(value)` |
| Sibling to sibling | Through the common parent, using both of the above | Parent holds `filterText`, passes it to both siblings |
The second row looks like a contradiction, so note the detail. Data still flows down. The parent passes a function down, and the child calls it to report an event. The parent decides what to do with it. A later chapter covers this as inverse data flow.
Common errors
- Copying a prop into local state. Writing
useState(props.value)creates a second copy that stops following the parent after the first render. If the value should follow the parent, read the prop directly. - Mutating a prop object. As shown above, this fails silently and breaks the link between data and screen.
- Passing everything down "just in case." Handing the whole
productsarray toProductRowmakes the component depend on data it does not use. Pass the smallest value it needs. - Trying to read a child's data from the parent. If the parent needs a value, the parent should own it and give it to the child.
- Long chains of pass-through props. If a prop crosses five components that do not use it, the flow is correct but tedious. This is a real trade-off of the model. React has other tools for deep trees, such as composition and context. Consider them only after the chain hurts.
Check your understanding
Exercise
A ProductRow component runs product.stocked = true during render because a teammate wanted every row to look in stock. Name two problems this causes.
Show answer
It mutates an object that belongs to the parent, so every other component reading the same PRODUCTS entry sees the changed data. It also happens during render without telling React anything changed, so the screen and the data can disagree. Props should be treated as read-only, and the component should compute a new local value instead.
Exercise
SearchBar and ProductTable are siblings, and both need the same filterText. Why can't SearchBar pass it straight to ProductTable, and where should the value live?
Show answer
Data flows only from parent to child, so there is no direct sibling-to-sibling route. The value should live in their closest common parent, FilterableProductTable, which passes it down to both through props.
Exercise
A parent re-renders. Which components does React call again by default, and why does that follow from one-way data flow?
Show answer
The parent and all of its descendants, even those whose props did not change. Data only flows downward, so any child could depend on the parent's new values, and React re-renders in the same direction as the data. Tools like React.memo can skip children whose props are unchanged.