In React, data flows in a single direction — from parent components to child components.
This means that:
This is what we mean by one-way (or unidirectional) data flow.
React’s one-way data flow provides:
Let’s visualize it through a simple example.
// Parent Component
function Parent() {
const [message, setMessage] = React.useState("Hello from Parent!");
return (
<div>
<Child text={message} />
</div>
);
}
// Child Component
function Child(props) {
return <h2>{props.text}</h2>;
}
Parent component has a state variable message.Child via the text prop.Child component cannot change the text prop directly — it can only display it.Parent (state) → Child (props) → UI
Sometimes, a child needs to send information up to the parent — for example, when handling a user event.
Since the data can’t flow up directly, we use a callback function.
function Parent() {
const [count, setCount] = React.useState(0);
const increment = () => setCount(count + 1);
return (
<div>
<h1>Count: {count}</h1>
<Child onIncrement={increment} />
</div>
);
}
function Child({ onIncrement }) {
return <button onClick={onIncrement}>Increase</button>;
}
increment function to update its state.increment down to the child as a prop (onIncrement).Parent (state) → Child (callback) → Parent (update) → Child (re-render)
| Feature | One-Way Data Flow (React) | Two-Way Data Binding (e.g., AngularJS) |
|---|---|---|
| Data Flow Direction | Parent → Child | Parent ↔ Child |
| Control of Data | Centralized in parent | Shared between parent and child |
| Predictability | High | Lower (due to multiple data sources) |
| Performance | Generally better | Can be slower with complex dependencies |
React purposely chooses one-way flow because it makes your app state more predictable and easier to debug.
Here’s a simple conceptual diagram:
[Parent Component]
↓ props
[Child Component]
↓ props
[Grandchild Component]
If the grandchild wants to update data, it must trigger a function defined in the parent.
Think of React’s one-way data flow like a waterfall:
One-Way Data Flow in React:
Latest tech news and coding tips.
The software industry is one of the most competitive places to build a career. Every…
How to Build APIs That Are Easy to Use, Scale, and Maintain Learn on the…
Almost everyone starts learning JavaScript with the wrong expectations. Let's fix them. Download the Codeflare…
Phaser JS is a powerful, open-source HTML5 game development framework used for creating 2D games that…
JavaScript / Node.js Authentication Libraries 1. Passport.js One of the most popular authentication middleware libraries…
Every profession comes with its own set of tools. A carpenter has a toolbox, a…