Redux is a powerful state management library for JavaScript applications, particularly those built with React. It provides a predictable state container that helps manage application state in a way that is scalable and maintainable. To fully leverage the power of Redux, it is essential to understand its core concepts: Store, Actions, and Reducers. In this section, we will delve into these concepts and explore the Redux Store API in detail.

Understanding the Redux Store

The Redux Store is the central hub for managing state in a Redux application. It holds the entire state tree of the application and serves as the single source of truth. The store is created using the createStore function provided by Redux. This function takes a reducer as an argument, which specifies how the state changes in response to actions.

Here is a basic example of creating a Redux store:

import { createStore } from 'redux';

// Define a simple reducer
const reducer = (state = {}, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    default:
      return state;
  }
};

// Create the Redux store
const store = createStore(reducer);

In this example, we define a simple reducer that handles an INCREMENT action. The store is then created using this reducer. The store provides several methods that allow interaction with the state, including getState, dispatch, and subscribe.

Exploring the Store API

The Redux Store API provides a set of methods that allow you to interact with the state in a controlled manner. Let's explore these methods:

1. getState()

The getState method returns the current state of the application. This method is useful for accessing the state at any point in time. Here's how you can use it:

const currentState = store.getState();
console.log('Current State:', currentState);

By calling getState, you can retrieve the entire state tree managed by Redux, which can then be used for various purposes, such as rendering the UI or performing conditional logic.

2. dispatch(action)

The dispatch method is used to send actions to the store. Actions are plain JavaScript objects that describe what happened in the application. They must have a type property, which indicates the type of action being performed. Here is an example of dispatching an action:

const incrementAction = { type: 'INCREMENT' };
store.dispatch(incrementAction);

When an action is dispatched, the store calls the reducer with the current state and the action being dispatched. The reducer returns a new state, which becomes the current state of the application.

3. subscribe(listener)

The subscribe method allows you to register a listener function that will be called whenever the state changes. This is useful for updating the UI or triggering side effects in response to state changes. Here's how you can use it:

const unsubscribe = store.subscribe(() => {
  console.log('State has changed:', store.getState());
});

The subscribe method returns a function that can be called to unsubscribe the listener. This is important for preventing memory leaks, especially in applications with dynamic components.

Actions: The Driving Force

Actions are the driving force behind Redux applications. They are payloads of information that send data from your application to the Redux store. As mentioned earlier, actions are plain JavaScript objects that must have a type property. This property is a string that describes the action being performed.

In addition to the type property, actions can contain other data that is necessary for the state update. For example, if you are building a to-do application, an action to add a new task might look like this:

const addTaskAction = {
  type: 'ADD_TASK',
  payload: {
    id: 1,
    text: 'Learn Redux',
  },
};

Actions are typically created using action creators, which are functions that return action objects. This approach makes it easier to manage and test actions. Here's an example of an action creator:

const addTask = (id, text) => ({
  type: 'ADD_TASK',
  payload: { id, text },
});

Reducers: The State Transformers

Reducers are pure functions that specify how the application's state changes in response to actions. They take the current state and an action as arguments and return a new state. Reducers must be pure, meaning they should not mutate the state or have any side effects.

Here's an example of a reducer for managing a list of tasks:

const tasksReducer = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TASK':
      return [...state, action.payload];
    case 'REMOVE_TASK':
      return state.filter(task => task.id !== action.payload.id);
    default:
      return state;
  }
};

In this example, the reducer handles two action types: ADD_TASK and REMOVE_TASK. When an ADD_TASK action is dispatched, the new task is added to the state. When a REMOVE_TASK action is dispatched, the specified task is removed from the state.

Reducers can be combined using the combineReducers function provided by Redux. This function allows you to split the state management logic into smaller, more manageable pieces:

import { combineReducers } from 'redux';

const rootReducer = combineReducers({
  tasks: tasksReducer,
  // Other reducers can be added here
});

const store = createStore(rootReducer);

Conclusion

Understanding the core concepts of Redux—Store, Actions, and Reducers—is crucial for building robust and scalable applications. The Redux Store API provides powerful methods for interacting with the state, while actions and reducers define how the state changes in response to user interactions and other events.

By mastering these concepts, you can harness the full potential of Redux to manage state in your React applications effectively. As you continue to explore Redux, you'll discover advanced patterns and techniques that can further enhance your application's architecture and performance.

Now answer the exercise about the content:

What is the primary function of the Redux Store in a Redux application?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Redux Core Concepts: Store, Actions, and Reducers: Role of Middleware in Enhancing Store Capabilities

Next page of the Free Ebook:

36Redux Core Concepts: Store, Actions, and Reducers: Role of Middleware in Enhancing Store Capabilities

7 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text