Redux State Management for React: A Comprehensive Guide

Microservice architecture is the new trend in the modern age of front-end development. The approach involves splitting various data flows/ separating the concerns of your application into the components. While this introduces many benefits and creates a degree of simplicity, it can also create complexity when several components depend on the same data.

React is a front-end library. Redux is managing the application’s state holds to a single global object called the store, and solves where the props need to be passed deep down into the component tree which is also called the ‘prop drilling’ problem. Redux’s fundamental principles help in maintaining consistency throughout the application, which makes debugging and testing easier.

Redux is a simple JavaScript library that helps to manage states in any JavaScript-based application such as React, Angular, Vue, etc.

Prerequisite:

  • Familiarity with React core concepts
  • Familiarity with async JavaScript

What is Redux?

The Redux pattern has several defining characteristics:

  • The state of your whole application is stored in an object tree within a single store (Single source of truth).
  • The store is immutable (it cannot be directly changed – instead changes create a new copy of the store).
  • Actions are dispatched on a shared message bus, handled by pure functions called reducers. Reducer is a pure function that takes state and action as arguments, returning a new state from the initial state.

REDUX data flow

The following figure shows the data flow of Redux state management:

View

Let us understand what is application state management and the different types of application states first. State management is the idea of sharing application data across multiple components within the same application where each component can read and modify the application state depending on the requirement. We have been emitting the event from here.

React component uses application state to render the portions of UI which have undergone changes without refreshing the complete page. The application state can be divided into two:

  • Local state – it’s the state which is used only inside a particular component and not being shared across any other components.
  • Global state – it’s the state which is being initialized in one parent component shared across multiple components which can be a direct child or part of a different component tree.

We can get the data from the store and the state gets the instantiation of the selector into the use Selector method from react-Redux library.

Import {use Selector} from ‘react-Redux’;
const username = useSelector(({auth}) => {
Return auth. Username
)

Action

Action is a simple JavaScript object that is used to send (dispatch) information from React components to other building blocks. It describes all the changes in the state of the application after the user interaction, internal events, and API calls.

Here’s the code of the Action object

{
type: “USER_LOGIN”,
Payload: {username: “John Doe”, password: “123456”}
}

Action must have ‘type’ property and optional payload data which should be communicated to other blocks. Action payloads can be read by reducers.

We can call the action object with the instantiation dispatch of the use Dispatch method from the react-Redux library

import {use Dispatch} from ‘react-Redux’Const dispatch = useDispatch()

//dispatching to the action creators functions
dispatch(authLogin(userCred))

Reducers

Reducers read the payloads from the actions and then update the store accordingly. It’s a pure function to return a new state from the initial state.

Function authReducer(state, action) {
switch (action.type) {
case ‘USER_LOGIN’:
return {…state,
auth: action. payload
}
default:
return state
}
}

Store

A store is an immutable object tree in Redux. A store is a state container that keeps the application’s state. Redux can have only one store in your application. Whenever a store is created in Redux, you need to specify the reducer. UI will be rendered automatically when the subscribe a selector call-back in a Redux store.

The store is created using the creator.
Store method from the Redux library

import {createStore } from “redux”;const store = createStore(authReducer)

You can pass it to the reducer function as a parameter. Now you are ready to dispatch an action that is managed by the reducer:

let userCred =

{ username: “johndoe”, password: ‘123456’ };

store.dispatch(authLogin(userCred));

The finalization of providing the store to be available for your application to our react components by putting a React Redux in the src/index.js file.
We can create the Provider selectors from the React-Redux library.

 

import { Provider } from ‘react-redux’import AppView from ‘./AppView’;

import { store } from ‘./store’

const App = () => {

return (

)

}

export default AppThis is how we start state management with React applications using React and React-Redux.

Conclusion

Redux is one of the most used libraries in state management for React applications. Redux has a lot of middleware libraries available in React such as Redux-Saga and Redux thunk, it’s especially used in handling API calls and side effects. We can write a standard way to Redux logic using ReactJS/toolkit. I hope this article will be a good first step in creating your own React application using Redux, React-Redux.

 

Wahbe Rezek

Berater, KI & Deep Tech

Wahbe, mit Sitz in Amsterdam, verfügt über einen soliden Hintergrund im Projekt- und IT-Change-Management, insbesondere bei der Stadt Amsterdam und ING. Im Jahr 2019 wechselte er als Programmmanager in die Abteilung Financial Markets von ING und spezialisierte sich auf KI. Seit Ende 2022 hat Wahbe Future Focus gegründet, das KI-Beratungs- und Implementierungsdienste anbietet und Kunden dabei unterstützt, das Potenzial der künstlichen Intelligenz voll auszuschöpfen. Darüber hinaus ist er als Advisor-AI & Deep Tech bei Innovature tätig, wo er strategische Einblicke und Beratung zu modernsten KI-Technologien bietet.

Image of Wahbe Rezek

Jesper Bågeman

Partner, Technologie

Jesper ist ein IT-Enthusiast, der sich dafür einsetzt, durch Technologie positive Veränderungen voranzutreiben. Er leitet mit drei Kernprinzipien: Aufbau echter Partnerschaften mit Kunden, Integration von Nachhaltigkeit in den Betrieb und Priorisierung der Stärkung und des Wohlbefindens von Teammitgliedern. Jespers Engagement für diese Werte stellt sicher, dass er wirkungsvolle Ergebnisse liefert.

Image of Jesper Bågeman

Tiby Kuruvila

Chefberater

Tiby ist ein angesehener Technologieexperte, der für seine Beiträge im Projektmanagement und in der Technologieentwicklung bekannt ist. Sein Engagement für den technologischen Fortschritt und das Management von Kundenbeziehungen hat ihn zu einem wertvollen Mitarbeiter für die Förderung des Geschäftswachstums und die Aufrechterhaltung der Kundenzufriedenheit in verschiedenen Sektoren gemacht.

Image of Tiby, on of Innovature's Co-founders

Meghna George

Personalleiter

Meghna widmet sich der Gestaltung von HR-Praktiken und der Förderung einer Kultur des Wachstums und der Ermächtigung, um Innovature in eine glänzende Zukunft zu führen. Mit einem beeindruckenden Hintergrund im Personalwesen hat Meghna erfolgreich HR Shared Services geleitet und das HRBP-Portfolio für große Serviceeinheiten verwaltet. Ihre Expertise umfasst strategische Planung, Change Management und Mitarbeiterentwicklung, was sie zu einer entscheidenden Kraft für die Förderung organisatorischer Exzellenz macht.

Image of Meghna George, the HR manager

Unnikrishnan S

Vizepräsident

Unnikrishnan bringt einen reichen Erfahrungsschatz in der Durchführung wirkungsvoller Softwareprojekte und der Umsetzung strategischer Technologiesinitiativen mit. Seine umfassenden Kenntnisse in Projektmanagement, Betrieb und Kundenbindung führen durchweg zu bedeutenden Ergebnissen und machen ihn zu einem vertrauenswürdigen Führer im IT-Bereich.

Image of Unnikrishnan S, Vice President of Innovature

Gijo Sivan

CEO, Global

Gijo hat seinen Sitz in Japan und verfügt über zwei Jahrzehnte Erfahrung in modernen Webtechnologien, Big Data-Analysen, Cloud Computing und Data Mining. Er spielt eine entscheidende Rolle bei der Gestaltung des globalen Rufs des Unternehmens, insbesondere in der japanischen IT-Branche, und bringt umfassende Erfahrungen in den Bereichen Vertrieb, Delivery Management, Partner Management, Betrieb und Technologieberatung mit.

Image of Gijo Sivan, Global CEO of Innovature

Ravindranath A V

CEO, Indien & Amerika

Ravindranath ist ein erfahrener Manager, der für seine globale Expertise in IT-Strategie, Infrastruktur und der Bereitstellung von Software-Services bekannt ist. Mit Fokus auf Innovation übersetzt er Geschäftskonzepte von Kunden in umsetzbare Lösungen für verschiedene Branchen wie Bankwesen, Einzelhandel, Bildung und Telekommunikation.

Image of Ravindranath, CEO of Innovature Americas