Skip to content

Commit 299e9c5

Browse files
authored
immer-reducers.md better rule for state mutations
Update Linting State Mutations paragraph with better rule for slice files
1 parent ae59d5f commit 299e9c5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

docs/usage/immer-reducers.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,19 @@ const itemsSlice = createSlice({
453453

454454
Many ESLint configs include the https://eslint.org/docs/rules/no-param-reassign rule, which may also warn about mutations to nested fields. That can cause the rule to warn about mutations to `state` in Immer-powered reducers, which is not helpful.
455455

456-
To resolve this, you can tell the ESLint rule to ignore mutations to a parameter named `state`:
456+
To resolve this, you can tell the ESLint rule to ignore mutations and assignment to a parameter named `state` only in slice files:
457457

458458
```js
459-
{
460-
'no-param-reassign': ['error', { props: true, ignorePropertyModificationsFor: ['state'] }]
459+
// @filename .eslintrc.js
460+
module.exports = {
461+
...
462+
overrides: [
463+
...,
464+
{
465+
files: ['src/**/*.slice.ts'], // feel free to replace with your preferred file pattern - eg. 'src/**/*Slice.ts'
466+
rules: { 'no-param-reassign': ['error', { props: false }] }, // avoid state param assignment
467+
},
468+
],
461469
}
462470

463471
```

0 commit comments

Comments
 (0)