-
-
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
no-direct-mutation-state do not fail in constructor #832
Comments
Yes, that should warn. |
@ljharb could you please explain why? If I understand correctly state is initialized in the constructor, so it can't be mutated. |
@TrustNik it doesn't cause the same problems as mutating I can see the argument that the original spirit of this rule doesn't apply to "within the constructor", but allowing mutation isn't worth making an exception imo. |
@ljharb I see. Thanks for clarification. |
You should never call The correct linting rule would be to remove the warning for |
It is correct to warn about any mutation. |
Except for setting state in the constructor. This behavior is against the official docs and some people think they should use the https://facebook.github.io/react/docs/state-and-lifecycle.html#do-not-modify-state-directly |
@ivosabev Assigning to |
I have this rule Implemented here https://github.com/burabure/eslint-plugin-burabure/blob/master/lib/rules/no-direct-mutation-state.js @ljharb would you like this merged? |
@burabure if you have a pr with tests that can fix any broken behavior, that'd be great. |
was this ever fixed? i'm still receiving a warning when modifying |
@ryndshn assigning to it once is fine; modifying it afterwards is intentionally disallowed. |
Why is it disallowed? I don't see any reason not to, and "never a good thing" is not a good explanation. How do you modify state in constructor then, if it was initially assigned to in class properties? |
You don’t. You assign your entire state all at once, in one place. If you can’t do that in a class property, you shouldn’t have a “state” class property at all - do it all in the constructor. |
Hello,
I guess following code should not be considered as a problem (now it is):
What do you think?
The text was updated successfully, but these errors were encountered: