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.
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…