Skip to content

Custom Middleware

Custom Middleware sits at the heart of middleware in Redux. This guide walks through the concept step by step, with examples, a cheatsheet, and common mistakes to avoid.

Custom Middleware Overview

At its core, custom middleware is about doing one thing well in Redux. Once you understand the pattern, you can apply it consistently across projects and teams.

Good custom middleware pays off across the whole project: fewer surprises, easier collaboration, and smoother onboarding. The snippet below is a solid starting point.

import { createListenerMiddleware } from '@reduxjs/toolkit';
import { increment } from './counterSlice';

export const listener = createListenerMiddleware();

listener.startListening({
  actionCreator: increment,
  effect: async (action, api) => {
    console.log('incremented', api.getState());
  },
});

Listener middleware reacts to dispatched actions to run side effects, replacing many custom thunks.

Custom Middleware Example

const slice = createSlice({ name, initialState, reducers });
const store = configureStore({ reducer: { key: slice.reducer } });
// dispatch(slice.actions.something())
  • Start from a minimal Custom Middleware example and grow it only as needed.
  • Keep things explicit so Custom Middleware behaves the same for everyone on the team.
  • Name things clearly so teammates understand your Custom Middleware at a glance.
  • Verify Custom Middleware works as expected before relying on it in important work.

Redux Cheatsheet

Quick Redux Toolkit reference related to custom middleware.

Concept Example Purpose
Store configureStore({ reducer }) Hold app state
Slice createSlice({ name, reducers }) State + actions together
Read state useSelector((s) => s.x) Get data in components
Dispatch useDispatch() Send actions
Async createAsyncThunk(...) Handle side effects
Data fetching createApi(...) RTK Query endpoints
Derived data createSelector(...) Memoized computations

How Custom Middleware Works in Redux

Custom Middleware follows Redux's predictable data flow: components dispatch actions, reducers compute the next state from the previous state and the action, and subscribed components re-render.

Listener middleware reacts to dispatched actions to run side effects, replacing many custom thunks.

  • State lives in a single, read-only store.
  • Actions are the only way to describe a change.
  • Reducers are pure functions that return the next state.
  • Redux Toolkit removes most boilerplate with slices and thunks.

Practical Guidance for Custom Middleware

In modern apps, custom middleware should use Redux Toolkit rather than hand-written Redux. Keep state minimal, colocate logic in slices, and use RTK Query for server data.

Concern Recommendation
Boilerplate Use Redux Toolkit (createSlice)
Server data Prefer RTK Query over manual thunks
Performance Memoize selectors with createSelector
Types Use typed useAppSelector/useAppDispatch hooks

Common Mistakes

  • Skipping edge cases and error handling when using custom middleware.
  • Not verifying the result of custom middleware before moving on.
  • Over-complicating custom middleware before you actually need the extra flexibility.
  • Ignoring documentation, which makes custom middleware hard for the next person to follow.

Key Takeaways

  • Custom Middleware is a core part of working effectively with Redux.
  • Start small and keep custom middleware focused on a single goal.
  • Apply consistent patterns so custom middleware scales across your project.
  • Practise and document custom middleware to keep your workflow maintainable.

Pro Tip

Bookmark this custom middleware pattern and reuse it. Consistency across your Redux work is worth more than clever one-off solutions.