DEV Community

Cover image for Information Flow in React
Cindy Hernandez
Cindy Hernandez

Posted on • Updated on

Information Flow in React

What is information flow in React?

Information flow refers to the way data is passed around between components in a React application. In React data flows in one direction - from the parent component to its child components. This is known as unidirectional data flow.


Since data only flows in one direction, it's easier to track where it comes from and where it's going. This makes the application more predictable and easier to maintain over time.


How does information flow work in React?

In React, data is passed from the parent component to its child components through props. Props are read-only and cannot be modified by the child component. This means that if a child component needs to modify the data, it needs to notify its parent component by passing a callback function


Example:

Here we have a parent component called App and the child component below called Header.

App has a state variable called isDarkMode, which functions as a boolean (true or false).

The onToggleDarkMode function and isDarkMode state variable is then passed to the Header component as a prop.


App

When the button is clicked it calls the onClick function that we see in the header component that allows the user to toggle from light to dark mode.


Header

Benefits of unidirectional data flow:

  1. With data flow in one direction it makes it easier to debug and maintain overtime

  2. Components can be reused in different parts of the application

  3. Unidirectional data flow makes it easier helps make it easier to break the application into smaller and manageable components.


Sources:

  1. React Information Flow, Flatiron Learning Module

Top comments (0)