Data flow mental map in React
π§ 1. The Core Decision Rule (Golden Rule)
Whenever you're building something, ask:
π βWho owns this data?β
That determines everything.
β 2. When to Send Data (Parent β Child)
β Use this when:
Child just needs to display or use data
Child does not need to modify it
π‘ Example
function Parent() {
const user = "Pushpesh";
return <Child name={user} />;
}
function Child({ name }) {
return <h1>Hello {name}</h1>;
}
β Real-life thinking
πΊ Like a TV:
Parent = Remote control
Child = Screen
Screen just shows what it's told
β Developer Signal
If your thought is:
"Child just needs to show or use something"
π β Send data as props
βοΈ 3. When to Send Functions (Parent β Child)
β Use this when:
Child needs to trigger or update parent data
There is state in parent
π‘ Example
function Parent() {
const [count, setCount] = React.useState(0);
return <Child increment={setCount} />;
}
function Child({ increment }) {
return (
<button onClick={() => increment(prev => prev + 1)}>
Click Me
</button>
);
}
β Real-life thinking
ποΈ Like a hotel bell:
Parent = Manager
Child = Receptionist
Receptionist doesnβt change hotel policy, but can:
βHey manager, update this!β
β Developer Signal
If your thought is:
"Child needs to change something that's not inside itself"
π β Pass a function
π₯ 4. When NOT to Pass Anything
β Keep data inside child when:
It's local
No other component needs it
π‘ Example
function Child() {
const [isOpen, setIsOpen] = React.useState(false);
}
β Developer Signal
Ask yourself:
"Does anyone else care about this data?"
β No β keep in child
β Yes β lift to parent
β¬οΈ 5. Key Pattern: βLifting State Upβ
This is a standard React pattern
Problem
Two children need same data:
<ChildA />
<ChildB />
π Where should data live?
β Answer: Parent
Pattern
function Parent() {
const [data, setData] = React.useState("");
return (
<>
<ChildA setData={setData} />
<ChildB data={data} />
</>
);
}
β Real-life example
π¨βπ©βπ§ Family budget:
Parent holds money info
Child A updates spending
Child B displays balance
π 6. Full Flow Pattern (Most Important)
β Standard pattern used in real apps:
function Parent() {
const [value, setValue] = React.useState("");
return (
<>
<Input onChange={setValue} />
<Display value={value} />
</>
);
}
function Input({ onChange }) {
return (
<input onChange={(e) => onChange(e.target.value)} />
);
}
function Display({ value }) {
return <p>{value}</p>;
}
π₯ Whatβs happening?
| Component | Role |
|---|---|
| Parent | Owns data |
| Input Child | Sends updates |
| Display Child | Shows data |
π§ 7. Decision Tree (Use This While Coding)
Step 1:
π Who owns the data?
Parent β keep in parent
Child only β keep in child
Step 2:
π Does child need to modify it?
β No β pass data
β Yes β pass function
Step 3:
π Do multiple components need it?
- β Yes β lift state to common parent
βοΈ 8. Advanced Standard Patterns (Good to Know)
As you grow, you'll use:
β 1. Controlled Components (Forms)
Parent controls form data
<input value={value} onChange={handleChange} />
β 2. Callback Pattern
Child sends data upward via function
onSubmit(data)
β 3. Prop Drilling (less ideal)
Passing data through many layers
π Solved by Context API / Redux
π§© 9. Simple Mental Model (Best One)
π Think of React like a Google Form system:
Parent = database
Input field = child
Display = child
π Flow:
Input updates parent
Parent stores data
Parent sends updated data to display
β Final Cheat Sheet
| Situation | What to Do |
|---|---|
| Child needs data | Pass data |
| Child needs to change parent data | Pass function |
| Data used in many places | Lift state up |
| Data only used locally | Keep in child |
π One-Line Rule (Interview Ready)
βIn React, data flows top-down via props, and child components communicate with parents through callback functions passed as props.β