Understanding Redux Actions, Reducers, and Store

Tutorial 2 of 5

Introduction

In this tutorial, we'll delve into the core concepts of Redux: actions, reducers, and the store. By the end of this guide, you'll have a solid understanding of how these components interact within a Redux application.

You'll learn how to:
- Define and dispatch actions
- Create reducers to handle actions
- Initialize and update the Redux store

Prerequisites:
- Basic understanding of JavaScript
- Familiarity with ES6 syntax
- Some experience with React is helpful but not required

Step-by-Step Guide

Understanding Actions

In Redux, actions are JavaScript objects that contain information about an event that has occurred in the application. They must have a type property, which indicates the type of action performed.

{ 
  type: 'ADD_TODO', 
  payload: { 
    text: 'Learn Redux' 
  } 
}

Understanding Reducers

Reducers are functions that determine changes to an application's state. They consider the current state and the dispatched action. After that, they return a new state.

function todoReducer(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return [...state, action.payload];
    default:
      return state;
  }
}

Understanding the Store

The store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it.

import { createStore } from 'redux';

let store = createStore(todoReducer);

store.dispatch({
  type: 'ADD_TODO',
  payload: { text: 'Understand the Redux store' }
});

Code Examples

Creating a Simple Redux Store

import { createStore } from 'redux';

function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
}

let store = createStore(counter);

store.subscribe(() => console.log(store.getState()));

store.dispatch({ type: 'INCREMENT' }); // Output: 1
store.dispatch({ type: 'INCREMENT' }); // Output: 2
store.dispatch({ type: 'DECREMENT' }); // Output: 1

Summary

In this tutorial, we've covered the core concepts of Redux. We've learned about actions, reducers, and the store. We also looked at creating a simple Redux store and updating it using dispatch.

Further learning steps could involve exploring middleware in Redux, learning how to integrate Redux with a UI library like React, or delving into asynchronous action handling with Redux Thunk or Redux Saga.

Practice Exercises

  1. Create a Redux store that manages a list of tasks. Implement actions for adding a task, deleting a task, and toggling a task's completion status.

  2. Extend the previous exercise by adding error handling. Implement actions and reducers for handling errors, such as trying to delete a task that doesn't exist.

Remember to test your solutions by dispatching actions and logging the updated state. For further practice, consider building a small project using Redux, such as a simple counter or a task manager.