GTB
GTB

Reputation: 57

Mutable state changes in Redux slice reducer

Can i return new primitive value in reducer? For example, i have 2 fields in state, tasks and filter, and, respectively, 2 reducers in combineReducers. Filter contains string value, initial value: "all". When action "CHANGE_FILTER" is dispatching, can i just return action.payload in filterReducer with new string value of filter:

export function filterReducer(state = "all", action) {
  switch (action.type) {
    case "CHANGE_FILTER":
      return action.payload;

    default:
      return state;
  }
}

action.payload is "all"/"active"/"completed". I know that i can't mutate state and must use spread operator and etc, but i don't want filter field to be object, i prefer primitive. PS: as far as i know with RTK i can do it

Upvotes: 0

Views: 182

Answers (1)

markerikson
markerikson

Reputation: 67459

Yes, returning a value in a reducer always works fine.

Upvotes: 1

Related Questions