-
-
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
Handle UpdateExpressions in no-direct-mutation-state #1387
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.
Seems legit, pending comment. Thanks!
code: [ | ||
'class Hello extends React.Component {', | ||
' constructor() {', | ||
' this.state.foo = 1;', |
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.
Could we also add a test that makes ++
invalid inside the 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.
Oh shoot, I must have missed adding that line checking another thing… Sorry.
I had this.state.foo++
right here as a valid case - we're in the constructor and while I think it's awkward, it's legal.
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 don't see why it should be legal; it's mutating this.state
after it's assigned.
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.
Assignment doesn't actually matter until we're out of the constructor
method. That's when React takes over.
But that's also a tangential change to what I wanted to make here - I'm bringing ++
to parity with =
(you have a test case immediately above this one showing that). If you want to change how you treat assignment as well, that's fine but I think you probably want to do that distinctly.
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.
Fair enough.
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.
This has been my favorite find in our codebase where we were assigning to state.
this.setState({
foo: this.state.foo++
});
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.
@TheSavior (cc: @zpao )
I actually have a PR proposing a rule catching all references to this.state
used in at this.setState
in #1374. Does this rule cover that as well? If not, I would be really happy for feedback.
This rule should also catch increment & decrement operators.
fb6b216
to
aafafc4
Compare
Do you need anything else from me here? |
This rule should also catch increment & decrement operators.
Fixes #1386