-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
no-did-mount-set-state with arrow functions & React.Component? #1595
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
Comments
This seems related to #1597 |
You are correct, your code should trigger this rule. The problem:
In your example componentDidMount is declared as an AssignmentExpression and not a Property or a MethodDefinition. This is why it is not triggered, and we need to check for that as well. If someone wants to make a contribution, please feel free, or else I will have a look at it when I find time. |
(It's actually a really bad idea to put functions in class properties; but the rule should still handle it) |
@np-8 first of all, React lifecycle methods never need to be bound to the class, so it's just wasting performance and clarity. That linked article talks about event handlers only. The article's example of constructor-bindings for instance methods is in fact the best possible solution (based on stage 3 proposals and above); using arrow functions in class properties isn't equivalent (for testing, performance, or clarity). |
Thanks. I was not aware that the lifecycle methods behave differently. I though any method could be bound to React.Component using the arrow function notation. I come from Python where there is clear distinction between classes and instances. To me, in JS everything looks the same (not sure if the methods are for the React.Component class or instances of that class). |
Any method can be bound. The issue is whether it has to be, because in JS, the |
Yeah, I understand. So finding this "bug" is not neccessarily very useful :) Thanks for the clarifications! |
It's still useful for people who misuse class properties in this way :-) just not for the specific use case you found. Thanks for reporting! |
Hello,
If I am correct, using no-did-mount-set-state should trigger with the following code:
For some reason, I do not get any errors from ESLint. The actual rule I am using is
'react/no-did-mount-set-state': [2, 'disallow-in-func']
.Question
Should the rule trigger with the code above?
System setup
Windows 10, node v.8.9.3, eslint-plugin-react v. 7.5.1, ESLint v.3.19.0 & babel-eslint 8.0.3.
The text was updated successfully, but these errors were encountered: