-
-
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
Allow this.state assignment on constructor #1223
Allow this.state assignment on constructor #1223
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add more invalid examples, including assignment to this.state inside a callback in the constructor, as well as in other lifecycle methods?
@@ -1,8 +1,10 @@ | |||
# Prevent direct mutation of this.state (react/no-direct-mutation-state) | |||
|
|||
NEVER mutate `this.state` directly, as calling `setState()` afterwards may replace | |||
Don't mutate `this.state` directly, as calling `setState()` afterwards may replace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this line needs to be changed; assignment isn't mutation.
|
||
Object.keys(list).forEach((key) => { | ||
if (isValid(list[key])) { | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be simpler to invert the if and put the report inside
the mutation you made. Treat `this.state` as if it were immutable. | ||
|
||
The only place that it's acceptable to assign this.state is on a ES6 class component constructor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/on/in, and add a period at the end. Also lets backtick class
bbb3313
to
886b7a0
Compare
@ljharb ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Could we also include an invalid test case like:
constructor() {
super();
doSomethingAsync(() => {
this.state = 'bad';
});
}
@@ -49,11 +49,18 @@ module.exports = { | |||
// -------------------------------------------------------------------------- | |||
// Public | |||
// -------------------------------------------------------------------------- | |||
var inConstructor = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use let
here.
886b7a0
to
09a7f93
Compare
@ljharb ready for review! |
09a7f93
to
36c7ade
Compare
👍 |
Has this been released yet? |
No. You can tell by clicking on the commit hash - 61b65a0 - and seeing that it's in master, but there's no version tags that it's in. |
@ljharb Thanks |
I was under the impression |
Nope; React would have no way to know you'd done that, so it wouldn't trigger a rerender. You should only change state with |
@ljharb I have updated the plugin to 7.3.0 and I still get this error :( |
@supra28 i'm not sure what error you mean. can you file a new issue? |
see #832