What is the recommended way to deal with no-param-reassign in reducers?
See original GitHub issueThis problem has been discussed before, but I haven’t seen a general agreement on the best way to solve it.
Although this reduce()
operation (as a whole) is pure, the callback within it is not, so it breaks no-param-reassign
:
const arr = [['fred', 24], ['bob', 32]];
const obj = arr.reduce((accumulator, item) => {
accumulator[item[0]] = item[1]; // ERROR! no-param-reassign
return accumulator;
}, {});
// obj === { fred: 24, bob: 32 }
Option 1 – suppress the linter:
const obj = arr.reduce((accumulator, item) => {
accumulator[item[0]] = item[1]; // eslint-disable-line no-param-reassign
return accumulator;
}, {});
Option 2 – make a new object each time:
const obj = arr.reduce((accumulator, item) => (
{ ...accumulator, [item[0]]: item[1] }
), {});
Neither option feels great.
- Suppressing the linter should be restricted to unusual edge cases imo, not everyday idiomatic JavaScript like
.reduce()
. - Creating a new object on every iteration creates extra garbage. A programmer can take one look at the operation and see that it’s pure. I realise that technically it’s possible that
arr.reduce
might refer to something other thanArray.prototype.reduce
, so the linter can’t make a special exception here. But it feels wrong to write slower code just because of a limitation in the linter.
What’s the best thing to do?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:10
Top Results From Across the Web
How to avoid no-param-reassign when setting a property on a ...
As this article explains, this rule is meant to avoid mutating the arguments object. If you assign to a parameter and then try...
Read more >no-param-reassign - ESLint - Pluggable JavaScript Linter
Rule Details This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
Read more >What the Heck is the deal with no-param-reassign
The first thought when people see the no-param-reassign error is to copy the parameter value into a locally scoped variable. view plaincopy to ......
Read more >Redux Toolkit | Codetain - end-to-end software development
Long story short - in this file, first define the initial state of the reducer with the correct type. Then use the createSlice...
Read more >Writing Reducers with Immer | Redux Toolkit - JS.ORG
Many ESLint configs include the https://eslint.org/docs/rules/no-param-reassign rule, which may also warn about mutations to nested fields. That ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
“creates extra garbage” is a memory concern, and in a memory-managed language, that should be none of the programmer’s. Create a new object on every iteration, which makes the reducer pure (as well as the overall
reduce
, which is pure in both cases).However,
ignorePropertyModificationsFor
is already enabled in the next release ofeslint-config-airbnb-base
, so if your accumulator is named one of the things it’s enabled for, it will be allowed.You’re not describing something a JS developer should be thinking about. GC is negligible most of the time - you don’t need to “code really precisely” (something most competent developers do on the web), you just need to write clean code.
If you close a tab and your RAM doesn’t go down, then clearly that’s a bug in the browser - no amount of attempted memory management in JS can fix that.
You clearly prefer to write your JS code as if it was C, tightly controlling memory usage. Go for it! This guide does not endorse or encourage that mindset, nor will it.