-
Notifications
You must be signed in to change notification settings - Fork 26.7k
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
prefer-stateless with event handlers is problematic #784
Comments
This is a current bug in For now, I'd just disable the rule. |
This should be fixed now in eslint-plugin-react. The next release of the linter config will definitely include it (if the current one doesn't already) |
You could have set it up like this:
|
@barrymichaeldoyle while that's fine for that use case (passing it into a div), for a custom component that would be bad because it'd create a new function every render - the only option there is a class-based component. |
Ah right, good point. |
@ljharb I'm new to react, still trying to learn the ins and outs of react. Could you elaborate on your response with an example? That will be greatly appreciated :) thanks |
@obouchari you can see in the example here that every render - every time |
Thanks for the awesome explanation. Correct me if I'm wrong. So in the case of using a custom child component. Every time MyComponent rerenders the child component will rerender as a result since the returned function will be different every time. And that will cause a performance issue? What about creating a brand new function every time we render MyComponent, isn't that a problem as well? Just curious 😄 |
@obouchari if you create it on every render then that’s a problem; but the constructor of a class component isn’t called on every render. |
Got you! Thanks, that was really helpful. |
I noticed with the latest version of the config that I get a lot of prefer-stateless warnings on components that just use props and event handlers. However, this is hard to do when the
react/jsx-no-bind
rule is enabled, which stops you from making functions for event handlers in the render path.For example, here's a component that only uses props and an event handler that the linter will tell me to turn into a stateless component.
However, in the attempt to do so, you need to create a function in the render path, which causes the
react/jsx-no-bind
lint rule to blow up on you.You can work around this by defining the function outside of the JSX, but at that point you're just working around the linter.
The text was updated successfully, but these errors were encountered: