You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Do we want to recommend the no-param-reassign eslint rule in the docs? #521
changed the title [-]Do we want to recommend the `no-param-reassign` rule in the docs?[/-][+]Do we want to recommend the `no-param-reassign` eslint rule in the docs?[/+]on Apr 24, 2020
Only if you override the default configuration of { "props": false } to { "props": true }.
Over in immerjs/immer#189, mwestrate essentially recommends the rule in its default config.
Long time user of redux here, new to RTK / Immer. Running into ESLint complaining that I'm reassigning state in createSlice. But this is fine though, right, because RTK is using produce under the hood? If so, how do you recommend using the no-param-reassign rule? I know you can do ignorePropertyModificationsFor, but in some cases I'd like to just completely rewrite state in the reducer, not just a property, for example if we're holding a loading on/off boolean.
Assigning state = never does anything useful. All that does is change what the local variable state is pointing to, which is always a no-op.
If you want to return a completely new state value / replace the existing state, you need to actually return it: return action.payload.
Reacted by Md. Rafee, Harsh boricha, Dave Esaias, riskers, Asim Kattum Thazha, Vinicius Pacheco Franco, Luke Ramus, Yogendra, Rinat, Bryan Primus and 3 more
@StuartMorris0 that is most definitely not the standard eslint setup, as the props option to that rule defaults to false.
Are you extending some overly restrictive eslint config like the airbnb config? (Which in my personal opinion has outlived it's usefulness and is not a good default).
A correct configuration would be just no-param-reassign: "error" or no-param-reassign: ["error", { "props": false }] (which is equalivalent)
Reacted by Stuart Morris, Mark Erikson, Michał Wanat, Mateusz Gruźla, Adam Mullin, Pierre Criulanscy, Robert Górnicki, Philippine Sentilhes, vlad, nastassia-dev and 25 more
Reacted by Emre, gsouveton, Joon and Rodrigo Viera
Reacted by Claudiu Cojocaru, Vadim Civ, Harsh boricha, Emre, gsouveton, Joon, 𝕾𝖆𝖒𝖚𝖗𝖆𝖎 𝕿𝖊𝖈𝖍𝖑𝖊𝖆𝖉 and Azamat
You're right it is the airbnb extends causing the issue, I have reverted the rule to its default. "no-param-reassign": ["error", { "props": false }]
Thanks
Reacted by Mateusz Gruźla, Konstantin Lebedev, Hunmin Park, Luca Ziliani, Stepan, Brian Orora, Majurageerthan, Gustavo Azevedo, crissCM, CavidM and 10 more
All my reducers are in a single directory called reducers.
Then I want to force the eslint to disable no-param-reassign for the reducers directory only.
Then I create a new eslint config file(.eslintrc.json) inside the reducers directory, and add "no-param-reassign": ["error", { "props": false }] to it.
so the .eslintrc.json inside reducers directory is like below
Reacted by Vladislav Puzyrev, Robert Kleger, Jonathan Mohrbacher, ali-target, AbdelMounhim, Reza, Christopher Bussick, Ruslan Yurchenko, Lorenzo Pieri, Marco Túlio Terra and 35 more
Reacted by Ildar
Reacted by Vladislav Puzyrev, Jonathan Mohrbacher, José Neto, Mihály Sáróy, Álef Duarte, Denis Mitryakov, Ildar, Dennis Gonzales and Tamara Suárez
Reacted by Vladislav Puzyrev, Jonathan Mohrbacher, Adel Bassiony, Prabhat Thapa, DrummerHead, nastassia-dev, Álef Duarte, Denis Mitryakov, Anthony Fields, Ildar and 4 more
Reacted by Vladislav Puzyrev, Jonathan Mohrbacher, Francisco Masi, José Neto, Mihály Sáróy, Denis Mitryakov, Ildar, Dennis Gonzales, Gunawan Wahyu and Rayhan Kinan Muhannad
Reacted by Vladislav Puzyrev, sect, yuu, Lorenzo Pieri, José Neto, Denis Mitryakov, Dennis Gonzales and Rayhan Kinan Muhannad
Activity
[-]Do we want to recommend the `no-param-reassign` rule in the docs?[/-][+]Do we want to recommend the `no-param-reassign` eslint rule in the docs?[/+]markerikson commentedon Apr 24, 2020
Pretty sure that rule also forbids mutating
someParam.field = 123, which doesn't work well with Immer.phryneas commentedon Apr 24, 2020
Only if you override the default configuration of
{ "props": false }to{ "props": true }.Over in immerjs/immer#189, mwestrate essentially recommends the rule in its default config.
elramus commentedon May 6, 2020
Long time user of redux here, new to RTK / Immer. Running into ESLint complaining that I'm reassigning state in createSlice. But this is fine though, right, because RTK is using
produceunder the hood? If so, how do you recommend using theno-param-reassignrule? I know you can doignorePropertyModificationsFor, but in some cases I'd like to just completely rewrite state in the reducer, not just a property, for example if we're holding a loading on/off boolean.markerikson commentedon May 6, 2020
Assigning
state =never does anything useful. All that does is change what the local variablestateis pointing to, which is always a no-op.If you want to return a completely new state value / replace the existing state, you need to actually return it:
return action.payload.elramus commentedon May 6, 2020
Ohhh okay, thanks much.
phryneas commentedon May 6, 2020
And that's exactly why I suggest recommending this eslint rule xD
markerikson commentedon May 6, 2020
So what does it mean to "suggest" it in this case, exactly? Where would we document that info? How would we encourage people to do so?
phryneas commentedon May 6, 2020
Hmm, that's the question.
Maybe in the style guide, hand-in-hand with the
immerandRTKrecommendations, and/or in the documentation forcreateReducerandcreateSlice?markerikson commentedon May 6, 2020
That could work, yeah.
StuartMorris0 commentedon Jun 9, 2020
I'm interested to know what the proposed solution/information to be in the style guide would be here.
If you use the standard eslint setup and use an example from RTK such as:
You would get the reassignment errors on the
state.lines. What is the proposed solution here? Thanksphryneas commentedon Jun 9, 2020
@StuartMorris0 that is most definitely not the standard eslint setup, as the
propsoption to that rule defaults tofalse.Are you extending some overly restrictive eslint config like the airbnb config? (Which in my personal opinion has outlived it's usefulness and is not a good default).
A correct configuration would be just
no-param-reassign: "error"orno-param-reassign: ["error", { "props": false }](which is equalivalent)StuartMorris0 commentedon Jun 9, 2020
You're right it is the airbnb extends causing the issue, I have reverted the rule to its default.
"no-param-reassign": ["error", { "props": false }]Thanks
markerikson commentedon Jun 9, 2020
Yes, this, 💯 :)
StuartMorris0 commentedon Jun 10, 2020
Does anyone know of any other good defaults I could review please?
markerikson commentedon Jun 10, 2020
@StuartMorris0 I would start with https://www.npmjs.com/package/eslint-config-react-app .
BenjiTheC commentedon Nov 15, 2020
If Airbnb rule set has outlived its usefulness, is there a newly recommended one? Thx!
phryneas commentedon Nov 15, 2020
@BenjiTheC what about the defaults that ship with eslint?
Or the one linked directly above your question.
ackalhan commentedon Dec 30, 2020
What I normally do is
no-param-reassignfor thereducersdirectory only..eslintrc.json) inside thereducersdirectory, and add"no-param-reassign": ["error", { "props": false }]to it.so the
.eslintrc.jsoninsidereducersdirectory is like belowmarkerikson commentedon Mar 23, 2021
Now covered in our docs:
https://redux-toolkit.js.org/usage/immer-reducers#linting-state-mutations
stevedeighton commentedon Aug 19, 2022
l'm a bit late to the party but I create my slices with naming convention of
x.slice.ts, then use an override to only apply this rule to slices:{ "files": ["src/**/*.slice.ts"], "rules": { "no-param-reassign": "off" } }Luk-z commentedon Oct 13, 2022
better solution, update the docs with this logic ?
MikelArnaiz commentedon Dec 22, 2022
You should probably not turn off the rule completely, otherwise it won't warn you when reassigning
state. What I have is:no-param-reassign - reduxjs/redux-toolkit#521