-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect no-direct-mutation-state
Warning
#1812
Comments
The first one should be throwing at runtime, since |
What if I have: state = {};
constructor(props) {
super(props);
// No warning here
this.state.randomId = Math.round(Math.random() * 1000000);
this.props.tables.forEach((table, i) => {
if (table.isOpen) {
// Here we have a warning
this.state[`table${i}Shown`] = true;
}
});
} |
in that case, I would certainly expect a warning there. |
Actually there should not be warnings in any of both places |
The "direct state mutation" is able in any part of the constructor (sync) |
The rule only allows assigning this.state in the constructor. It is never OK to mutate it. |
Ok, so, clearly there is something that I'm missing.
Could you explain to me why I see a warning inside the forEach?
Thanks
… El 7 jun 2018, a las 02:44, Jordan Harband ***@***.***> escribió:
The rule only allows assigning this.state in the constructor. It is never OK to mutate it.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Sure - first, the Second, the rule is designed to prevent any mutation of the state object after it’s created. So, you should see a warning inside the forEach, but also on the randomId line. In other words, you should build up the entire state object before assigning it to the instance. |
Actually, I'm not seen and I should not see a warning in the randomId line. I got your point after checking what you said in #832 I disagree about warning in any place of the constructor. The react-state doesn't know anything about Thank you a lot for your time. |
The point of the rule is to forbid mutating state in the constructor ever. It does, also, have an effect - mutating an object after creation is slower than creating it all at once. |
Additionally, mutation of any kind is always a code smell, especially in react. If you want a different object, clone it and assign the new one. |
https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-direct-mutation-state.md
This is happening right now:
The text was updated successfully, but these errors were encountered: