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.
Why borrowing code is a skill—when you understand what you're copying. For years, "copy-paste developer"…
Interactive forms rarely keep every field active all the time. Sometimes an input should only…
A modern JavaScript API for working with dates, times, time zones, and calendars without the…
Understanding What Happens Behind the Scenes Node.js looks simple from the outside—you write JavaScript, call…
The need for absolute certainty is the greatest disease the engineering mind faces. The moment…
The software industry is one of the most competitive places to build a career. Every…