Redux Toolkit exports several type-safe action matching utilities that you can leverage when checking for specific kinds of actions. These are primarily useful for the builder.addMatcher() cases in createSlice and createReducer, as well as when writing custom middleware.
All these matchers can either be called with one or more thunks as arguments, in which case they will return a matcher function for that condition and thunks, or with one actions, in which case they will match for any thunk action with said condition.
isAsyncThunkAction - accepts one or more action creators and returns true when all match
isPending - accepts one or more action creators and returns true when all match
isFulfilled - accepts one or more action creators and returns true when all match
isRejected - accepts one or more action creators and returns true when all match
isRejectedWithValue - accepts one or more action creators and returns true when all match
A higher-order function that returns a type guard function that may be used to check whether an action is a 'pending' action creator from the createAsyncThunk promise lifecycle.
A higher-order function that returns a type guard function that may be used to check whether an action is a 'fulfilled'' action creator from the createAsyncThunk promise lifecycle.
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the createAsyncThunk promise lifecycle.
A higher-order function that returns a type guard function that may be used to check whether an action is a 'rejected' action creator from the createAsyncThunk promise lifecycle that was created by rejectWithValue.
// action is a rejected action dispatched by either `requestThunk1` or `requestThunk2`
// where rejectWithValue was used
}
}
#Using matchers to reduce code complexity, duplication and boilerplate
When using the builder pattern to construct a reducer, we add cases or matchers one at a time. However, by using isAnyOf or isAllOf,
we're able to easily use the same matcher for several cases in a type-safe manner.
First, let's examine an unnecessarily complex example: