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.
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…
Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…
What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…