Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redux Toolkit · Module

listenerMiddleware.stopListening

Removes a registered listener entry from Redux Toolkit listener middleware. It can optionally cancel active instances and reports whether a matching subscription was found.

How it works

stopListening removes a registered listener entry by matching the listener function reference together with its action type, action creator, matcher, or predicate. It returns whether a matching subscription was found, and cancelActive can additionally cancel any active instances of that listener.

Syntax

const stopListening = (options: AddListenerOptions & UnsubscribeListenerOptions) => boolean

Parameters

NameTypeDescription
optionsAddListenerOptions & UnsubscribeListenerOptionsListener matching options, using an action type, action creator, matcher, or predicate together with the listener/effect function reference.
cancelActivetrueWhen true, cancel currently running instances of the matched listener while removing it.

Returns

boolean: true when a matching listener entry was removed; false when no matching subscription was found.

Key points

  • stopListening unregisters one listener entry from the listener middleware.
  • Its matching configuration follows the same option shape used to register listeners.
  • Unregistration and cancellation are separate; active effects continue unless cancellation is requested.
  • Use cancelActive: true when removal must also cancel currently running listener effects.
  • A true result indicates that a matching listener entry was successfully removed.
  • A false result means no registered listener matched the supplied options.

Common mistakes

  • Removing a listener does not stop effect executions that are already running unless cancelActive: true is provided.
  • Matching depends on the original function references, so equivalent newly created functions will not identify the existing listener.

Example: Remove a listener and cancel active instances

import { createListenerMiddleware } from '@reduxjs/toolkit'
import { todoAdded } from './todos'

const listenerMiddleware = createListenerMiddleware()

const listener = () => {
  console.log('todo added')
}

listenerMiddleware.startListening({
  actionCreator: todoAdded,
  effect: listener,
})

const removed = listenerMiddleware.stopListening({
  actionCreator: todoAdded,
  effect: listener,
  cancelActive: true,
})

console.log(removed) // true if the matching listener was removed

This example registers a listener and then removes it by supplying the same matching field and listener function reference. Setting cancelActive also stops currently running instances.

When to use it

Unregister behavior that is no longer needed

Application lifecycle changes

Remove a listener while optionally cancelling its active effects

Cleanup and teardown

Switch which actions trigger reactive logic at runtime

Dynamic workflows

Interview questions

QWhat does stopListening do and what does it return?

stopListening removes a listener entry and returns true when a matching subscription was removed; otherwise it returns false. It accepts the same matching options as startListening, including an action type, action creator, matcher, or predicate, plus the listener function.

Follow-up: How would you ensure an in-flight effect is stopped too? Pass cancelActive: true in the stopListening options.

QHow is the listener selected for removal?

The match is based on the listener function reference and the supplied action-matching field. Recreating a function with identical behavior is not sufficient because it has a different reference.

Follow-up: What should be retained to reliably unregister a listener? Keep the original listener/effect function reference and reuse it when calling stopListening.

QHow does cancellation work when stopping a listener?

By default, removing the registration does not cancel active effect instances. Supplying cancelActive: true requests cancellation of those running instances as part of removal.

Follow-up: What happens if no matching registration exists? The method returns false and no listener entry is removed.

  • createListenerMiddleware
  • listenerMiddleware.startListening
  • listenerMiddleware.clearListeners
  • addListener
  • removeListener

Source: createListenerMiddleware - Redux. Checked 2026-10-15.