Stan aplikacji

state/immer-mutate-or-return

A Redux Toolkit or Immer reducer either mutates the draft or returns a new state, never both; no arrow that returns a mutation.

poważnastatycznefe-state

Dlaczego

Inside createSlice and createReducer, Immer lets a reducer "mutate" a draft. It also accepts a returned new state, but not both at once. An arrow with an expression body, such as (state, action) => state.push(action.payload), mutates the draft and returns the array length, and Immer throws when the action is dispatched.

Poprawka

Give the arrow a block body ((state, action) => { state.push(action.payload) }), or put void before the mutation. To replace the state instead, return a new value and leave the draft alone: (state, action) => state.filter((t) => t.id !== action.payload).

Przykłady

// bad
todoAdded: (state, action) => state.push(action.payload),
// good
todoAdded: (state, action) => {
  state.push(action.payload)
},
todoRemoved: (state, action) => state.filter((t) => t.id !== action.payload),

Źródła

Fakty

Identyfikator
state/immer-mutate-or-return
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
redux
Uczy jej skill
fe-state State and stores
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain state/immer-mutate-or-return