Group reducer matches
A reducer needs one case for several related actions.
Redux Toolkit · Function
isAnyOf creates a type-safe matcher that succeeds when any supplied action creator or matcher function matches an action. It is commonly passed to builder.addMatcher or used in custom middleware.
Call isAnyOf with action creators or matcher functions to obtain a type guard. The returned guard evaluates to true when at least one supplied condition matches the tested action, making it useful with builder.addMatcher and custom middleware.
isAnyOf(...matchers)| Name | Type | Description |
|---|---|---|
matchers | ActionCreator | TypeGuard ... | One or more action creators or matcher functions accepted by the same input rules as isAllOf. |
A type guard function that returns true when at least one supplied condition matches the tested action.
import { isAnyOf, createAction } from '@reduxjs/toolkit'
const added = createAction<{ id: string }>('items/added')
const removed = createAction<{ id: string }>('items/removed')
const isItemChanged = isAnyOf(added, removed)
const action = removed({ id: 'a1' })
if (isItemChanged(action)) {
console.log(action.payload.id)
}Creates a matcher that accepts either of two action creators and narrows the action type inside the conditional.
A reducer needs one case for several related actions.
Middleware should react to any action in a selected set.
TypeScript code needs safe narrowing after a match.
isAnyOf creates a type guard from multiple conditions. The guard returns true when at least one supplied condition matches the action.
Follow-up: How would you use it in a reducer? Pass the returned guard to builder.addMatcher so the reducer handles any matching action in one case.
It is useful when matching several action types without manually comparing action.type values. The resulting predicate also provides TypeScript narrowing for the matched action.
Follow-up: Why prefer a matcher predicate over a string comparison? The predicate can combine multiple conditions and preserve type-safe narrowing.
Call isAnyOf with the desired action creators or matcher functions, then invoke the returned function with an action. It returns a boolean type-guard result.
Follow-up: What happens if no condition matches? The guard returns false, so the matcher case is skipped.
isAllOfisAnyOfisFulfilledisPendingisRejectedbuilder.addMatchercreateReducercreateSlice