Lesson 4 of 16
Action Creators and Middleware Making Redux Extensible
Introduces action creators and middleware as core extensibility mechanisms in Redux, showing how intent is encapsulated, side effects are isolated, and async workflows are handled without breaking reducer purity.
Context / Motivation
In the last lesson, we built a working Redux store from scratch. We saw how actions describe what happened, reducers describe how state changes, and the store coordinates everything.
But as applications grow, you’ll start noticing two kinds of complexity creeping in:
- Repetitive action definitions — you keep writing the same
{ type, payload }objects everywhere. - Side effects — you need to fetch data, log actions, or handle async operations without cluttering your reducers.
To handle these problems gracefully, Redux introduced Action Creators and Middleware — two mechanisms that bring structure and extensibility without violating Redux’s purity principles.
Let’s explore them step by step.
Conceptual Explanation
1. Action Creators — Encapsulating Intention
An action creator is simply a function that returns an action object.
Without action creators, we dispatch actions manually:
store.dispatch({ type: "ADD_TODO", payload: "Learn Redux" });But soon we find ourselves repeating that pattern throughout the app. If the action structure changes, we’d need to update every call site.
With an action creator:
function addTodo(text) {
return { type: "ADD_TODO", payload: text };
}
// usage
store.dispatch(addTodo("Learn Redux"));Benefits:
- Consistency — all actions of a given type follow the same structure.
- Reusability — you can reuse the same action creator in many components.
- Encapsulation — changes to action shape happen in one place.
Think of action creators as verbs in your system’s language.
They express intent clearly: addTodo(), removeTodo(), toggleTodo().
The reducer then decides how those verbs affect state.
2. Middleware — The Hidden Layer Between Action and Reducer
Middleware in Redux acts as a pipeline stage between the time you dispatch an action and the time it reaches the reducer.
Why is this needed?
Because not all logic belongs inside reducers. Reducers must be pure functions — no side effects, no I/O, no randomness. But real-world apps need side effects: logging, async requests, analytics, etc.
Middleware provides a safe place for such logic.
3. How Middleware Works
Each middleware function sits in a chain. When an action is dispatched:
- It first goes through the middleware chain.
- Each middleware can observe, modify, delay, or even cancel the action.
- After passing through all middleware, the action reaches the reducer.
In pseudocode:
dispatch(action) → middleware1 → middleware2 → reducer → new state
Every middleware follows a consistent signature:
const exampleMiddleware = (storeAPI) => (next) => (action) => {
// code before passing action forward
const result = next(action);
// code after action is processed
return result;
};Here’s what each part means:
storeAPIgives access togetStateanddispatch.next(action)forwards the action to the next middleware or reducer.- You can perform side effects before or after forwarding.
4. Example: Logging Middleware
Let’s write a simple logging middleware to see it in action.
const logger = (store) => (next) => (action) => {
console.log("Dispatching:", action);
const result = next(action);
console.log("Next state:", store.getState());
return result;
};You attach middleware when creating the store:
import { applyMiddleware, createStore } from "redux";
const store = createStore(reducer, applyMiddleware(logger));Now every dispatched action will be logged before and after it’s processed.
5. Example: Async Middleware (Thunk)
What if you want to perform asynchronous operations like fetching data from an API? Reducers can’t handle async logic because they must be pure. This is where thunks come in — a special kind of action that is a function instead of an object.
function fetchTodos() {
return async function (dispatch) {
dispatch({ type: "FETCH_TODOS_START" });
const response = await fetch("/api/todos");
const data = await response.json();
dispatch({ type: "FETCH_TODOS_SUCCESS", payload: data });
};
}To make this work, we need a middleware that detects if an action is a function and executes it instead of sending it directly to the reducer.
That’s exactly what the famous redux-thunk middleware does:
const thunk = (store) => (next) => (action) => {
if (typeof action === "function") {
return action(store.dispatch, store.getState);
}
return next(action);
};Once applied, it lets you handle side effects like network requests elegantly, while keeping reducers pure and predictable.
Real-World Illustration
Let’s return to our shopping cart example.
You might need to:
- Log every cart update (for analytics).
- Fetch item details before adding to cart.
- Handle errors gracefully.
Without middleware, these side effects would invade your components or reducers. With middleware:
- A logger middleware logs actions for debugging.
- A thunk middleware allows async logic (e.g., fetching product details before adding to cart).
- A custom analytics middleware sends data to Google Analytics after certain actions.
This separation keeps each layer clean:
- Components handle user input.
- Middleware handles async and side effects.
- Reducers handle pure state updates.
Your app remains modular, testable, and predictable — even as it grows in complexity.
Evolution / Refinement
Redux’s flexibility through middleware was one of its earliest breakthroughs. It allowed developers to build ecosystems of enhancements — for logging, error handling, promises, routing, batching, and async flows.
However, writing boilerplate like applyMiddleware, defining multiple action creators, and manually managing async states (_START, _SUCCESS, _FAILURE) quickly became repetitive.
That’s exactly what Redux Toolkit (RTK) was designed to simplify:
- You no longer need to manually write action creators —
createSlice()generates them for you. - You no longer manually manage async actions —
createAsyncThunk()handles pending/fulfilled/rejected states automatically. - You no longer wire up middleware manually —
configureStore()sets up thunk and DevTools for you by default.
RTK preserves the middleware architecture but automates the scaffolding.
Principle / Takeaway
Redux’s design is powerful because it separates what happens (actions) from how it happens (reducers) and when it happens (middleware).
- Action creators: encapsulate intent in reusable functions.
- Middleware: intercept or extend Redux’s behavior safely.
- Reducers: remain pure and predictable.
Together, they make Redux both rigid in structure (predictable flow) and flexible in capability (custom middleware and async support).
This is what makes Redux a timeless architecture for managing complex UI state.
✅ Next Lesson Preview – Lesson 5: From Vanilla Redux to Redux Toolkit — Motivation and Design Goals We’ll now step into the modern age of Redux: why Redux Toolkit was created, what problems it solves, and how it preserves the same architecture with much less boilerplate.