The issue is the use of an arrow function with no curly braces as the reducer, because that acts as an implicit return statement. So, you’re both mutating state.token
, and returning the result of the assignment.
Per the Immer docs on returning data, there’s a couple ways to fix this:
- Adding the
void
operator in front of the assignment - Wrapping the assignment in curly braces to make it a function body
so setToken
reducer can be updated with void
as
setToken: (state, action) => void(state.token = action.payload.test)