Both MapStateToProps and useSelector work with similiar callback store => store.group
Is it safe to mutate those values for example in MapStateToProps like this:
const mapStateToProps = store => {
const { group, level } = store;
let { group } = store;
if (level > 50) {
group = `${group}-admin`;
}
return { group };
};
or in useSelector:
const group = useSelector(store => {
const { group, level } = store;
let { group } = store;
if (level > 50) {
group = `${group}-admin`;
}
return { group };
});
And with useSelector it could actually be done inside the component too like this:
let [group, level] = useSelector(store => [store.group, store.level);
if (level > 50) {
group = `${group}-admin`;
}
...
My co-worker did something like this and I'm not sure if you are supposed to use let
in there. I'm just interested if that is acceptable way to deal with this or if that causes problems? I don't need a different solution for it. I know how to do it with using const
instead.
One of the roles of
redux
is to centralize yourstate
logic:From: https://redux.js.org/introduction/getting-started
So when you do something like this:
This should be considered bad practice. The
useSelector
hook should return a piece of your state without mutating it. You are mutating it inside theuseSelector
call, so you are returning something that maybe not in thestate
object.From
node_modules/react-redux/src/hooks/useSelector.js
:See that they even name the result of
selectedState = selector(storeState)
asselectedState
. And you are not selecting astate
on your call. You are mutating the result.While in your example you are just mutating a string that you read from the
state
, this is a bad practice because one day you might mutate thestate
object without mutating it through anaction/reducer
dispatch. Which would definitely break your code at some point.For example:
What I think you should do:
SomeComponent.js
If what you want instead is to update the
group
state property on Redux, you should instead dispatch an action to be handled by a reducer.