0
0
Reactframework~10 mins

Parent-child data flow in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parent-child data flow
Parent Component
Child Component
Child renders UI
Parent updates state
Parent re-renders and passes new props
The parent sends data down to the child via props. The child uses these props to display or act. If the child needs to send data back, it calls a function from the parent, which updates the parent's state and triggers a re-render.
Execution Sample
React
import React, { useState } from 'react';

function Parent() {
  const [count, setCount] = useState(0);
  return <Child count={count} onClick={() => setCount(count + 1)} />;
}

function Child({ count, onClick }) {
  return <button onClick={onClick}>Count: {count}</button>;
}
Parent holds count state and passes it plus a click handler to Child. Child shows count and calls handler on click to update Parent.
Execution Table
StepParent State (count)Props Passed to ChildChild Render OutputAction Taken
10{count:0, onClick:function}<button>Count: 0</button>Initial render
20{count:0, onClick:function}User clicks buttonChild calls onClick
31{count:1, onClick:function}<button>Count: 1</button>Parent state updated, re-render
41{count:1, onClick:function}User clicks buttonChild calls onClick
52{count:2, onClick:function}<button>Count: 2</button>Parent state updated, re-render
62{count:2, onClick:function}No further clicksExecution stops
💡 No more user clicks, so no state changes or re-renders occur.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count01222
Key Moments - 2 Insights
Why does the child component update its displayed count when the parent state changes?
Because the parent passes the updated count as a prop to the child on each render (see execution_table rows 3 and 5). The child re-renders with new props automatically.
How does the child send data back to the parent to update the count?
The parent passes a function (onClick) as a prop to the child. When the child calls this function (execution_table rows 2 and 4), the parent updates its state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the value of count in the parent state?
A0
B1
C2
Dundefined
💡 Hint
Check the 'Parent State (count)' column at step 3 in the execution_table.
At which step does the child component call the parent's onClick function for the second time?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Child calls onClick' in the 'Action Taken' column in execution_table.
If the parent did not pass the onClick function as a prop, what would happen when the button is clicked?
AThe count would increase anyway
BThe child would update its own count state
CNothing would happen because child cannot update parent state
DAn error would occur immediately
💡 Hint
Refer to how data flows from parent to child and how child calls parent's function in execution_table.
Concept Snapshot
Parent-child data flow in React:
- Parent holds state
- Parent passes data and functions as props to child
- Child uses props to render UI
- Child calls parent's function prop to send data back
- Parent updates state and re-renders
- Data flows down, events flow up
Full Transcript
In React, the parent component holds state and passes data to the child component using props. The child uses these props to display information. If the child needs to update the parent's state, it calls a function passed down from the parent as a prop. When the parent state changes, React re-renders the parent and child with updated props. This creates a clear one-way data flow from parent to child, and event flow from child to parent.