What is Redux?

Karnika Gupta
Women in Technology
3 min readJul 13, 2023
Image: Redux logo.

Redux is a state management library for JavaScript applications. It is mostly used with UI frameworks such as React or Angular, to manage the state of an application in a centralised and predictable manner. Redux aims to simplify state management and unidirectional data flow in a complex application.

Core Components of Redux

  1. Store: It is the central object that holds the state of the application. Redux library provides a ‘createStore()’ function to create the store. It holds the current state and provides several methods of interaction like ‘getState()’ to access the store and ‘dispatch()’ to trigger state changes.
  2. Actions: These are simple JavaScript objects that describes an intention to change the state. They represent the interaction that occur in an application. An action has a ‘type’ field that describes the type of action and an optional ‘payload’ field carrying additional data.
  3. Reducer: They are pure functions responsible for transforming the state based on the dispatched action. It takes the current state and an action as input and return a new state. Reducers define how state should be modified to different actions. It is important to note that the reducers should be pure functions so that they do not mutate the state directly, instead return a new state object.
  4. Dispatch: It is the process of sending the action to the store. When an action is dispatched, the store forwards it to the reducer. Each reducer receives the action along with the current state and returns a new state based on the type of action. Redux ensures that the reducers are called in a predictable order, and the state updates happen in a controlled manner.
  5. Selectors: They extract the exact piece of data that is required from the state. They help in accessing the state in a structured manner. Selectors help in retrieving and computing derived data from the state, allowing components to access the relevant parts of the state that are required. By using selectors, you can avoid directly accessing the state of the store from components.
  6. Middleware: Redux middleware sits between dispatching an action and the moment it reaches the reducer. It allows you to add custom logic or behaviour to the dispatch process. Middleware can intercept actions, modify them, dispatch additional actions, or perform asynchronous operations.

Advantages of Redux

  • Predictable state management: Redux uses a strict unidirectional data flow, making it easier to understand and reason about how the state changes in an application.
  • Centralized state: The entire application state is stored in a single object within the store, making it easier to inspect and debug the application’s state at any given point.
  • Time-travel debugging: Redux’s pure functions and immutable state updates allow for powerful debugging techniques like time-travel debugging. Developers can replay actions and observe how the state changes at different point of time.
  • Easier testing: With redux, testing becomes simpler as the state changes are decoupled from the UI components. You can test reducers and selectors independently to make sure that they behave as expected.

Redux has gained popularity due to its scalability and maintainability for larger applications. However, it’s worth noting that redux may introduce additional complexity for smaller applications, and it’s not always necessary to use it. The decision to use redux should be based on the specific needs of the application and the complexity of state management.

--

--