react-template icon indicating copy to clipboard operation
react-template copied to clipboard

ESLint rule 'no-param-reassign' and Redux Toolkit

Open m208 opened this issue 3 years ago • 0 comments

ESLInt warns about reassigning state in createSlice()

const langSlice = createSlice({
  name: 'language',
  initialState,
  reducers: {
    setLanguage (state, action) {
      state.currentLang = action.payload;   // Error!
    },
  },
});

Suggestion: add rule as mentioned in RTK docs https://redux-toolkit.js.org/usage/immer-reducers#linting-state-mutations

{
  'no-param-reassign': ['error', { props: true, ignorePropertyModificationsFor: ['state'] }]
}

m208 avatar Nov 15 '22 16:11 m208