Group async-thunk handling
Middleware or reducers need to handle pending, fulfilled, or rejected actions from a selected group of async thunks.
Redux Toolkit · Function
Returns a type-guard matcher for actions created by createAsyncThunk. Pass thunk creators to restrict matches to those thunks, or omit them to recognize async-thunk actions generally.
isAsyncThunkAction is a higher-order matcher that returns a TypeScript type guard. With async thunk action creators supplied, the returned function narrows actions to those produced by any of those thunks; without supplied creators, it tests whether an action belongs to an async thunk lifecycle.
isAsyncThunkAction(...asyncThunkCreators)| Name | Type | Description |
|---|---|---|
...asyncThunkCreators | AsyncThunkActionCreator[] | Optional async thunk action creators used to restrict which thunk-generated actions match. |
A matcher function/type guard that returns true when the action is an async-thunk action matching the supplied creators, or any async-thunk action when none are supplied.
import { isAsyncThunkAction } from '@reduxjs/toolkit'
import type { UnknownAction } from '@reduxjs/toolkit'
import { requestThunk1, requestThunk2 } from './thunks'
const isRequestAction = isAsyncThunkAction(requestThunk1, requestThunk2)
export function handle(action: UnknownAction) {
if (isRequestAction(action)) {
action.type
}
}Create a matcher for actions generated by either supplied async thunk.
Middleware or reducers need to handle pending, fulfilled, or rejected actions from a selected group of async thunks.
Application logic needs to detect async-thunk lifecycle actions without maintaining action-type string lists.
TypeScript code needs action narrowing after checking the origin of an action.
It is a higher-order function that returns a type guard for actions created by createAsyncThunk. It can be called with thunk action creators to restrict matching to those thunks.
Follow-up: How would you match every async-thunk action? Call it without thunk arguments and use the returned matcher on the action.
Provide one or more async thunk action creators, then invoke the returned matcher with an action. The matcher returns true when the action was generated by one of those thunks.
Follow-up: What benefit does the returned type guard provide in TypeScript? It narrows the action inside the conditional branch to a recognized async-thunk action.
The utility supports both a creator-filtered form and a general form. The creator-filtered form matches actions from the supplied thunks, while the general form matches an async-thunk lifecycle action.
Follow-up: Which API commonly generates the actions being recognized? Redux Toolkit's createAsyncThunk API generates the thunk and its lifecycle actions.
createAsyncThunkisPendingisFulfilledisRejectedisRejectedWithValueisAllOfisAnyOf