Reputation: 741
We're using Redux.js with React.js (React-Redux), and in the reducer we're using the following code (we are using redux-actions to reduce boilerplate):
const update = require('react/lib/update');
const myReducer = handleActions
({
[myAction]: (state, action) => {
state = update(state, {
something: {$set: !state.someProperty}
});
return someFunction(state);
}
})
We are using update
from React Immutability Helpers but assigning the result into the state using state =
.
Is this against the basic Redux guidlines - i.e. mutating the state? It seems so, but the code seems to work perfectly and quickly, and the redux devtools shows the state changes correctly...
Upvotes: 0
Views: 734
Reputation: 67439
Your example is okay, because you're not actually mutating the contents of the object that the state
variable points to. Instead, you're just updating the local variable named state
to point to a different object instead.
It's important to understand how variables and references work in Javascript, which will help clear up what's going on here.
Personally, I'd suggest doing const newState = update(....)
for clarity.
Upvotes: 1