9

Hello I have a problem in my estlint:

Assignment to property of function parameter 'state'. eslintno-param-reassign

on this code:

state.sideisOpen = action.payload;

interface SideBar {
  sideisOpen: boolean;
}

const INITIAL_STATE: SideBar = {
  sideisOpen: true,
};

const sideBar = createSlice({
  name: 'toggleSide',
  initialState: INITIAL_STATE,
  reducers: {
    toggleSide: (state, action: PayloadAction<boolean>) => {
      state.sideisOpen = action.payload;
    },
  },
});

export const { toggleSide } = sideBar.actions;
export { sideBar };

1 Answer 1

18

Please try to edit your .eslintrc file to make the rule less strict for your case:

// .eslintrc
'no-param-reassign': ['error', {
      props: true,
      ignorePropertyModificationsFor: [
        'state',
      ]
    }],
3
  • is it possible to fix the error without placing this rule?
    – gabriel
    May 3, 2020 at 9:48
  • I think it's not possible, but please let SO community know if you find a solution. May 3, 2020 at 14:17
  • Oddly, this solution worked if I was only modifying an existing array element (e.g. state[someIndex] = action.payload), but did NOT work when adding new elements (e.g. state = [...state, action.payload]). I tried using ignorePropertyModificationsForRegex and setting props: false, but I eventually just had to set this rule to off completely. :/
    – morphatic
    May 3, 2020 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.