简体   繁体   中英

React-Native: should I unbind functions when unmounting components?

I am currently looking to improve my app performances. Based on this example:

class MyComponent extends React.PureComponent {

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

  doWork() {
    // doing some work here.
    // this.props.dispatch....
  }

  render() {
    return <Text onPress={this.doWork}>Do Some Work</Text>
  }

}

Should I unbind function this.doWork in componentWillUnmount() ? Is React Native auto unbinding functions?

You will need to unbind events that is created by you example, a scroll event, or a keyboard show event.

But not native events by native components like onPress event by <Text> as they will be handled automatically by react when the component is unmounted

This isn't necessary. Once the instance of the class is garbage collected, so are the bound functions to that instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM