# One-Way Data Flow

> State flows like a waterfall: strictly top-down. Siblings cannot talk directly.

Subject: React | Lesson 5 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/one-way-data-flow
Previous: [Virtual DOM & Reconciliation](https://codewithanimation.in/react/virtual-dom-reconciliation.md) | Next: [Functional Components](https://codewithanimation.in/react/functional-components.md)

## What it is

In React, data flows in exactly one direction: down the tree from parent to child via **props**. This is known as One-Way Data Flow.

A child component cannot pass data upwards to its parent, nor can it pass data sideways to a sibling component. If a component is given data via props, it cannot mutate that data; props are strictly read-only.

## Why it matters

This strict flow makes your application predictable and easy to debug. If a piece of UI is rendering incorrect data, you know exactly where to look: you trace the props up the tree until you find the component that owns the state.

In frameworks with two-way data binding, tracking down what mutated a variable can be a nightmare because any component can change any data.

## Lifting State Up

If two sibling components (e.g., an Input and a Display) need to share the same data, you cannot pass data directly between them.

Instead, you must **Lift State Up** to their closest common ancestor. The parent component holds the state and passes it down to both siblings via props. If a child needs to update the state, the parent passes down an update function as a prop.

## Common mistake: The sibling communication trap

A common beginner mistake is trying to make two sibling components talk to each other directly using refs or custom event emitters. This fights React's architecture.

If components need to share data, the data belongs in a parent. If the parent is too far up the tree resulting in 'prop drilling', you should use React Context or a state management library.

## Tip: The waterfall mental model

Think of state as a waterfall. Water can only flow down the mountain. If two distinct pools need access to the same water, you have to move the water source higher up the mountain so it flows down into both.

## Try it: Lifting State Up

Observe how data flows. Siblings cannot communicate directly. To share data, the state must be lifted to their common ancestor and passed down.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/one-way-data-flow

## Check yourself

### Question 1

Which direction does data flow in a React application?

- A. From parent to child (top-down).
- B. From child to parent (bottom-up).
- C. Bidirectionally (two-way binding).

Correct answer: A

Data in React is always passed down the tree via props. This one-way flow makes tracing bugs much easier.

- Why not B: Children cannot pass data up. They can only invoke functions passed down to them.
- Why not C: React deliberately avoids two-way data binding to maintain predictability.

### Question 2

How do two sibling components share state?

- A. By passing props directly between them.
- B. By lifting the state up to their closest common parent.
- C. By using the `useSibling` hook.

Correct answer: B

To share data between siblings, you must 'Lift State Up' to their common parent, which then passes the data down to both siblings.

- Why not A: Siblings cannot pass props to each other. Props only go from parent to child.
- Why not C: There is no such hook in React.

### Question 3

Can a child component modify the props it receives from its parent?

- A. Yes, by directly reassigning the prop variable.
- B. No, props are strictly read-only.
- C. Only if the parent marks the prop as `mutable: true`.

Correct answer: B

Props are read-only. If a child needs the data to change, it must call a callback function provided by the parent, asking the parent to update its own state.

- Why not A: Props are strictly read-only. Attempting to mutate them will cause errors.
- Why not C: There is no such setting. Props are always immutable.

## Remember this

- Data flows strictly top-down via props.
- Props are read-only and cannot be mutated by children.
- To share state between siblings, Lift State Up to their closest common ancestor.
