Redux-toolkit: Confused regarding `rejectWithValue` and `action.error`

Created on 25 Mar 2021  路  2Comments  路  Source: reduxjs/redux-toolkit

I am a bit confused on how to use the value provided from rejectWithValue

I was expecting that in my reducer/rejected reducer I would have access to an error?

{
  name
  message
  code
  stack
}

But I am always seeing just a value 'rejected'.
I would like UI to be dependent on the code of the error we are getting but am unsure of how to derive this value when returning rejectWithValue with and error from an async thunk.

Any help appreciated

Most helpful comment

Thank you for the clarification @msutkowski

All 2 comments

@stuartflanagan When you use rejectWithValue, the error is returned in the payload.

if the promise resolved with a rejectWithValue(value) return value, dispatch the rejected action with the value passed into action.payload and 'Rejected' as action.error.message

Reference: https://redux-toolkit.js.org/api/createAsyncThunk#return-value

From what I can tell from your description, you'd want to parse the error and return an expected/standardized format.

Thank you for the clarification @msutkowski

Was this page helpful?
0 / 5 - 0 ratings