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.
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…
Bubble Sort is one of the simplest sorting algorithms in computer science. Although it’s not…