REDUX 101: Terminologies

PART II

ยท

5 min read

REDUX 101: Terminologies

The core components of Redux include:

  1. Store:

    The central data store that holds the state tree of the entire application. It is typically a JavaScript object that represents the application's state.

  2. Actions:

    Plain JavaScript objects that describe changes in the application's state. Actions typically have a type and, sometimes, additional data (payload) that is used to update the state.

  3. Reducers:

    Functions that specify how the application's state changes in response to actions. Reducers are pure functions, meaning they produce the same output for the same input and do not have side effects.

    In Redux, a reducer is a pure JavaScript function that specifies how the application's state changes in response to actions. Reducers take two arguments: the current state and an action, and they return a new state. Reducers are a fundamental part of the Redux architecture and are crucial for managing the state of your application.

    A typical reducer function in Redux follows a specific pattern. Here are the key components of a reducer function:

    1. State: The first argument to a reducer is the current state of the application. It represents the data and state of the application at a specific point in time.

    2. Action: The second argument is an action object. This object typically has a type property, which describes the type of action to be performed, and optionally a payload or additional data that provides information about what needs to change in the state.

    3. Switch Statement: Reducer functions often use a switch statement to determine how to handle different action types. The type property of the action is used as the case in the switch statement to determine which part of the state needs to be updated.

    4. Immutability: Inside each case in the switch statement, the reducer should create a new state object rather than modifying the existing state. This is a fundamental principle in Redux, promoting immutability to ensure that the state remains predictable and traceable.

Here's a simplified example of a reducer function:

    const initialState = {
      count: 0,
    };

    function counterReducer(state = initialState, action) {
      switch (action.type) {
        case 'INCREMENT':
          return { ...state, count: state.count + 1 };
        case 'DECREMENT':
          return { ...state, count: state.count - 1 };
        default:
          return state; // Return the current state for unhandled actions
      }
    }

    export default counterReducer;

In this example, the reducer function counterReducer takes the current state and an action as input and returns a new state based on the action type. The new state is constructed by copying the existing state and making changes based on the action, ensuring immutability.

Reducers are typically combined and used with the Redux store to manage the state of an entire application. They play a critical role in keeping the application's state predictable and easy to manage.

  1. Dispatch:

    A method to send actions to the Redux store, triggering a state update.

  2. Subscribe:

    A method that allows components to listen to changes in the Redux store and update their UI when the state changes.

    A component subscribes to the Redux store using the useSelector hook, which is provided by the react-redux library.

Other Useful Components

Selector

  • Selectors are functions that are used to extract specific pieces of data from the Redux store state. They help in encapsulating the logic for computing derived state or extracting data in a way that is modular and maintainable. Selectors are especially useful when dealing with complex state structures or when you need to perform computations on the state.

  • While not part of the core Redux library, selectors are a common practice within the Redux ecosystem to manage and derive state in a structured and efficient way.

  • Selectors are functions that take the Redux state and return specific pieces of data from that state. They can help optimize rendering performance by memoizing computations and reducing unnecessary re-renders.

Redux Relation to Server State Management

Redux is primarily designed for client-side state management in React applications. It helps manage the state on the client side, providing a predictable state container that can be used to maintain and update the application's UI. While Redux itself doesn't directly handle server-side state management, it can play a role in managing the client-side state related to data fetched from the server.

Here's how Redux can interact with server-side state management:

  1. Data Fetching:

    • Redux is often used to manage the state of data fetched from a server. When data is retrieved from an API or other server-side source, Redux can store and manage that data in the client-side store.
  2. Asynchronous Operations:

    • Redux Thunk or other middleware can be used in conjunction with Redux to handle asynchronous operations, such as fetching data from a server. This enables developers to dispatch asynchronous actions to update the Redux store based on the results of server-side operations.
  3. Caching and Local Storage:

    • Redux state can be cached or stored in local storage to persist certain parts of the client-side state, providing an offline experience and reducing the need to fetch data from the server on every page load.
  4. Client-Side State Consistency:

    • While the primary responsibility of Redux is client-side state management, maintaining a consistent client-side state is crucial when interacting with server data. Redux helps ensure that the client-side state accurately reflects the server-side data.

However, the server-side state management itself is typically handled by server-side technologies, databases, and frameworks. Popular server-side technologies include Node.js, Django, Flask, Ruby on Rails, and others, depending on the programming language and environment.

In summary, while Redux is not designed for server-side state management, it plays a significant role in managing client-side state, especially when dealing with data fetched from servers or APIs. Developers often use a combination of client-side and server-side technologies to create seamless and responsive web applications.

ย