React.js / React State Management with Redux

Understanding Redux Actions, Reducers, and Store

In this tutorial, we'll delve into the core concepts of Redux: actions, reducers, and the store. We'll explain how they relate to each other and demonstrate how to use them in a p…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers managing global state with Redux and integrating it with React applications.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help