Functions in JSX

suggest change

For better performance, avoid creating a new arrow function inside JSX on each render.

As explained at https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md :

A bind call or arrow function in a JSX prop will create a brand new function on every single render. This is bad for performance, as it will result in the garbage collector being invoked way more than is necessary. It may also cause unnecessary re-renders if a brand new function is passed as a prop to a component that uses reference equality check on the prop to determine if it should update.

If you have JSX like this (TextInput uses onChangeText, not onChangeValue):

<TextInput
  onChangeText={value => this.handleValueChanging(value)}
/>

or

<button onClick={ this.handleClick.bind(this) }></button>

you can make it better:

<TextInput
  onChangeText={this.handleValueChanging}
/>

and

<button onClick={ this.handleClick }></button>

For the correct this inside handleValueChanging, bind it in the constructor:

constructor(props) {
  super(props);
  this.handleValueChanging = this.handleValueChanging.bind(this);
}

See also https://stackoverflow.com/questions/35446486/binding-a-function-passed-to-a-component.

Or use a helper such as autobind-decorator and add @autobind to each method you want bound:

@autobind
handleValueChanging(newValue) {
  // processing event
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents