Open In App

What’s the typical flow of data like in a React with Redux app ?

Redux is an open-source state management JavaScript library for managing the application state. It is popularly used in ReactJS, but is not limited to it, and can also be used with other JavaScript libraries such as Angular. In a conventional React-Redux application, there is a single store along with a single root reducer. As the application grows more complex, the root reducer is split into smaller reducers that operate on the different parts of the state tree.

Prerequisites:

For large-scale production-level applications, it turns out to be a complex task to reproduce issues and add new features if the application’s state is not managed efficiently and has no control over it. Redux reduces the complexity of the code by making the state-updating process easier.



Data Flow in a React-Redux Application

There are four fundamental concepts that govern the flow of data in React-Redux applications.

React-Redux Application Flow

Steps to Create React Application:

Step 1: Create a React application using the following command:



npx create-react-app react-with-redux

Step 2: After creating your project folder i.e. react-with-redux, move to it using the following command:

cd react-with-redux

Step 3: Install the required dependencies using this command in the terminal

npm i redux react-redux

Project Structure:

The updated dependencies in package.json file will look like.

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-qr-code": "^2.0.12",
"react-redux": "^8.1.3",
"react-scripts": "5.0.1",
"redux": "^4.2.1",
"web-vitals": "^2.1.4"
}

Example: This example uses Redux to show the data flow and state management in React Redux.




// Filename - App.js
 
import React, { Component } from "react";
import "./App.css";
import { GetMessage } from "./action/showMessageAction";
import { connect } from "react-redux";
class App extends Component {
    showMessage = () => {
        console.log("CALLING ACTION");
        this.props.getMessage();
    };
 
    render() {
        return (
            <div className="App">
                <header className="App-header">
                    <h3>
                        Flow of data in a React-Redux app
                    </h3>
                    <button onClick={this.showMessage}>
                        Click me to trigger an action!{" "}
                    </button>
                    <h5>
                        The updated state will be displayed
                        here:
                        {this.props.message}
                    </h5>{" "}
                </header>
            </div>
        );
    }
}
 
const mapActionsToProps = {
    getMessage: GetMessage,
};
 
const mapStateToProps = (state) => {
    return { message: state.data.message };
};
 
export default connect(
    mapStateToProps,
    mapActionsToProps
)(App);




// Filename - reducer/index.js
 
import { combineReducers } from "redux";
import showMessageReducer from "./showMessageReducer";
 
const combinedReducers = combineReducers({
    data: showMessageReducer,
});
 
export default combinedReducers;




// Filename - action/showMessageAction.js
 
export const SHOW_MESSAGE = "SHOW_MESSAGE";
 
export const GetMessage = () => {
    console.log("DISPATCHING ACTION");
    return {
        type: SHOW_MESSAGE,
        payload: { message: "Hello from GeeksforGeeks!" },
    };
};




// Filename - reducer/showMessageReducer.js
 
import { SHOW_MESSAGE } from "../action/showMessageAction";
 
const showMessageReducer = (
    state = { message: "No message" },
    { type, payload }
) => {
    switch (type) {
        case SHOW_MESSAGE:
            console.log("STATE UPDATION");
            return payload;
 
        default:
            return state;
    }
};
 
export default showMessageReducer;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: This output will be visible on the http://localhost:3000/ on the browser window.


Article Tags :