Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 20 of the Intermediate WordPress Plugins: REST API & React Admin course
WordPressJune 25, 20263 min read

Defining Actions and Reducers for WordPress Data Stores

Master the WordPress Data layer by implementing formal Actions and Reducers. Learn to manage state mutation predictably in your React-based admin dashboard.

WordPressReactReduxData LayerState Managementphpplugin-development

Previously in this course, we explored registering a custom data store and writing selectors for data access. Now that you have a store registered and a way to read data, it’s time to define how that data changes.

In the WordPress data architecture, you don't mutate state directly. Instead, you dispatch Actions—plain JavaScript objects describing what happened—which are then processed by Reducers—pure functions that determine how the state should update. This separation is the key to predictable state mutation.

The Anatomy of Actions and Reducers

Think of an action as an event notification. It contains a type (a string describing the action) and an optional payload (the data needed to perform the update).

A reducer is a function that takes the current state and an action, and returns the new state. Because reducers must be pure, they cannot modify the original state object; they must always return a new, updated version. If you've ever wrestled with React state management: Reducers vs. State Machines, you know that this functional approach is the best way to avoid side-effect bugs.

Creating Action Creators

While you could dispatch raw objects, we use Action Creators—functions that return these objects—to ensure consistency and simplify the dispatching process.

JAVASCRIPT
// store/actions.js

export function setKnowledgeBaseItems(items) {
    return {
        type: CE9178">'SET_ITEMS',
        items,
    };
}

export function updateItemStatus(id, status) {
    return {
        type: CE9178">'UPDATE_ITEM_STATUS',
        id,
        status,
    };
}

Defining Reducer Logic

Your reducer is the brain of your data store. It listens for the actions you just defined and updates the state accordingly. You should use a switch statement to handle different action types.

JAVASCRIPT
// store/reducer.js

const DEFAULT_STATE = {
    items: [],
};

export function reducer(state = DEFAULT_STATE, action) {
    switch (action.type) {
        case CE9178">'SET_ITEMS':
            return {
                ...state,
                items: action.items,
            };

        case CE9178">'UPDATE_ITEM_STATUS':
            return {
                ...state,
                items: state.items.map((item) =>
                    item.id === action.id 
                        ? { ...item, status: action.status } 
                        : item
                ),
            };

        default:
            return state;
    }
}

Notice how we use the spread operator (...state) to ensure we aren't mutating the existing state object. This follows the principles of TypeScript Immutability: Stopping Mutation Bugs in State Management by ensuring every state transition results in a new object reference.

Dispatching Actions to the Store

To trigger these updates from your React components, you use the dispatch function provided by the @wordpress/data registry.

JAVASCRIPT
import { useDispatch } from CE9178">'@wordpress/data';
import { STORE_NAME } from CE9178">'./constants';

const MyComponent = () => {
    const { setKnowledgeBaseItems } = useDispatch(STORE_NAME);

    const handleRefresh = async () => {
        const items = await fetchItems(); // Your service layer
        setKnowledgeBaseItems(items);
    };

    return <button onClick={handleRefresh}>Refresh Items</button>;
};

Hands-on Exercise

  1. Open your store/reducer.js file.
  2. Add a new action type REMOVE_ITEM to your reducer that filters the items array based on a provided id.
  3. Create a corresponding action creator in store/actions.js called removeItem(id).
  4. Dispatch this action from a "Delete" button in your Knowledge Base admin list component and verify that the UI updates instantly.

Common Pitfalls

  • Mutating State Directly: Never use state.items.push() or state.items[0] = value. Always return a new array or object. If you mutate, the WordPress data store won't trigger re-renders because it won't detect a change in reference.
  • Non-Serializable Payloads: Keep your action payloads simple. Avoid passing class instances or DOM nodes. Stick to plain JSON-serializable data (strings, numbers, arrays, objects).
  • Over-complicating Reducers: If your reducer logic is becoming massive, split it into smaller sub-reducers. A reducer should handle one slice of the state tree.

Recap

We've established that actions describe what happened, and reducers define how the state changes in response. By dispatching actions through the store, we ensure that our Knowledge Base plugin remains maintainable and that UI updates are strictly tied to state transitions.

Up next: Implementing Resolvers for Data Fetching.

Previous lessonWriting Selectors for Data AccessNext lesson Implementing Resolvers for Data Fetching
Back to Blog

Similar Posts

WordPressJune 26, 20264 min read

Handling Complex State Dependencies in WordPress Data Stores

Master complex state management by chaining selectors and coordinating cross-component dependencies in your WordPress Data store for consistent, clean UI logic.

Read more
WordPressJune 25, 20263 min read

Handling Asynchronous State in React for WordPress Plugins

Master React state management for asynchronous API requests. Learn to implement loading, error, and success states to create a seamless WordPress admin UI.

Part of the course

Intermediate WordPress Plugins: REST API & React Admin

intermediate · Lesson 20 of 45

  1. 1

    Setting up the WordPress Development Environment

    3 min
  2. 2

    Introduction to @wordpress/scripts

    3 min
  3. 3

    Configuring ESLint and Prettier

    3 min
Read more
WordPressReactJune 25, 20263 min read

Performance Optimization with Selectors in WordPress React Apps

Master React Performance Optimization in WordPress. Learn how to implement shallow comparison and memoized selectors to prevent unnecessary re-renders.

Read more
4

Localizing Data for JavaScript

3 min
  • 5

    Anatomy of a REST API Endpoint

    3 min
  • 6

    Implementing REST API Permission Callbacks

    3 min
  • 7

    Handling GET Requests in REST API

    3 min
  • 8

    Validating and Sanitizing API Arguments

    4 min
  • 9

    Creating POST Endpoints for Data Submission

    3 min
  • 10

    Updating Existing API Resources

    3 min
  • 11

    Handling Asynchronous State in React

    3 min
  • 12

    Building the Knowledge Base Service Layer

    3 min
  • 13

    Scaffolding the React Admin Dashboard

    3 min
  • 14

    Working with @wordpress/components

    3 min
  • 15

    Creating a React Form for Submissions

    3 min
  • 16

    Implementing CRUD in the Admin UI

    3 min
  • 17

    Understanding WordPress Data Store Architecture

    4 min
  • 18

    Registering a Custom Data Store

    3 min
  • 19

    Writing Selectors for Data Access

    3 min
  • 20

    Defining Actions and Reducers

    3 min
  • 21

    Implementing Resolvers for Data Fetching

    3 min
  • 22

    Optimizing Performance with Selectors

    3 min
  • 23

    Handling Complex State Dependencies

    4 min
  • 24

    Implementing Nonce Verification

    4 min
  • 25

    Advanced Sanitization Techniques

    Coming soon
  • 26

    Input Validation and Error Handling

    Coming soon
  • 27

    Protecting Admin Screens

    Coming soon
  • 28

    Production Build Pipeline

    Coming soon
  • 29

    Debugging React in the WordPress Admin

    Coming soon
  • 30

    Building Search and Filter Functionality

    Coming soon
  • 31

    Internationalization in React

    Coming soon
  • 32

    Managing File Uploads via REST API

    Coming soon
  • 33

    Optimizing API Response Times

    Coming soon
  • 34

    Working with Date and Time in React

    Coming soon
  • 35

    Implementing Drag-and-Drop Sorting

    Coming soon
  • 36

    Creating Custom Hooks for API Logic

    Coming soon
  • 37

    Integrating with Gutenberg Blocks

    Coming soon
  • 38

    Handling Conflict Resolution

    Coming soon
  • 39

    Building a Modal Confirmation System

    Coming soon
  • 40

    Implementing Activity Logging

    Coming soon
  • 41

    Using Webpack Aliases

    Coming soon
  • 42

    Unit Testing API Endpoints

    Coming soon
  • 43

    Unit Testing React Components

    Coming soon
  • 44

    Handling Large Datasets with GraphQL

    Coming soon
  • 45

    Implementing Real-time Updates with Web

    Coming soon
  • View full course