JS Built-ins
redux-toolkitcreateAsyncThunkintermediate

Handling Rejected State

πŸ“– Usage Example
builder.addCase(fetchUser.rejected, (state, action) => {
  state.error = action.error.message
  state.status = 'failed'
})

The rejected handler fires when the async function throws β€” use action.error.message to display the error to users.

Handling Rejected State

The rejected action fires when the thunk throws an error. Its action.error.message carries the error message string.

Challenge: Handle the rejected case to store the error message in state.error.

userSlice.reducer(undefined, fetchUser.rejected(new Error('not found'), 'req', 1))
// β†’ { error: 'not found', data: null }
πŸ’‘ Hints (3)
  • β€’ Use `fetchUser.rejected` in `builder.addCase` to handle errors.
  • β€’ `action.error.message` contains the error message string from the thrown Error.
  • β€’ Construct a rejected action for tests: `fetchUser.rejected(new Error("msg"), requestId, arg)`.