12 / 12

How can you listen to any dispatched action globally?

In Redux Toolkit, you can listen to every dispatched action globally by using middleware or by using the addMatcher function inside a slice’s extraReducers with a universal matcher like isAnyOf() or custom predicate functions.

Ways to Listen to Actions Globally
  1. 1

    Use custom middleware to intercept every dispatched action and perform side effects or logging.

  2. 2

    Use builder.addMatcher() in createSlice() to handle groups of actions based on conditions.

  3. 3

    Integrate listener middleware (createListenerMiddleware) to respond to specific or all actions declaratively.

Example 1: Using Custom Middleware
Example 2: Using Listener Middleware (RTK Built-in)

Using createListenerMiddleware is the most modern and recommended approach in Redux Toolkit, as it cleanly separates side effects from reducers and allows reacting to any action in a declarative way.