Skip to main content

Command Palette

Search for a command to run...

Data flow mental map in React

Updated
β€’5 min read

🧠 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:

  1. Input updates parent

  2. Parent stores data

  3. 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.”