coding-standards icon indicating copy to clipboard operation
coding-standards copied to clipboard

Add jsx-no-bind rule to config

Open kadamwhite opened this issue 8 years ago • 5 comments

For React rendering performance we should consider adding jsx-no-bind to this eslint config; bound functions and arrow functions created within a render method's JSX will never be treated as equivalent when React is comparing subcomponent properties and can lead to a lot of unnecessary (and therefore expensive) re-renders.

kadamwhite avatar Aug 18 '17 17:08 kadamwhite

The only thing I'd want to see here is that it does actually make a difference. In my experience, using pre-bound functions over arrow functions hasn't had any sort of significant or measurable effect on performance, compared to e.g. using PureComponent religiously.

It also strongly depends on whether create-react-app includes the property initialiser feature, since the generally recommended alternative approach is to bind there:

class Component extends React.Component {
    onClick = e => {
        e.preventDefault();
        this.props.onClick( e );
    }
}

Ergonomically, I still think arrow functions are the nicest, but property initialisers as about as good.

rmccue avatar Aug 20 '17 02:08 rmccue

It also strongly depends on whether create-react-app includes the property initialiser feature

It seems like it does support it:

In addition to ES6 syntax features, it also supports:

rmccue avatar Aug 20 '17 02:08 rmccue

Why not add it if it can help avoid unnecessary re-renders in child components?

The problem with this syntax is that a different callback is created each time the LoggingButton renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.

pdewouters avatar Oct 16 '18 19:10 pdewouters

Why not add it if it can help avoid unnecessary re-renders in child components?

Generally speaking, it's an unnecessary optimisation, so you just don't need to do it. Using property initialisers is cognitive overhead for negligible gain most of the time.

rmccue avatar Oct 17 '18 00:10 rmccue

Property initializer is more widely available now, and also more widespread, we can ban .bind. The main place it gets used is when the dev is unaware that property initializers would be a better solution.

kadamwhite avatar May 19 '20 15:05 kadamwhite