Unregister behavior that is no longer needed
Application lifecycle changes
Redux Toolkit · Module
Removes a registered listener entry from Redux Toolkit listener middleware. It can optionally cancel active instances and reports whether a matching subscription was found.
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.
const stopListening = (options: AddListenerOptions & UnsubscribeListenerOptions) => boolean| Name | Type | Description |
|---|---|---|
options | AddListenerOptions & UnsubscribeListenerOptions | Listener matching options, using an action type, action creator, matcher, or predicate together with the listener/effect function reference. |
cancelActive | true | When true, cancel currently running instances of the matched listener while removing it. |
boolean: true when a matching listener entry was removed; false when no matching subscription was found.
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 removedThis 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.
Application lifecycle changes
Cleanup and teardown
Dynamic workflows
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.
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.
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.
createListenerMiddlewarelistenerMiddleware.startListeninglistenerMiddleware.clearListenersaddListenerremoveListener