简体   繁体   中英

How to removeListener in reactjs es6

I use es6 in project of react.

componentDidMount() {
        userStore.addListener(ViewUpdateTypes.USER_UPDATE,(data)=>this._onChange(data));
        userStore.addListener(ViewUpdateTypes.FD_MENU_UPDATE,(data)=>this._onChange(data));
};

Now I want removeListener,What should I do.

You can do constructor and Function.prototype.bind like this.

constructor(props) {
    super(props);
    this._onChange = this._onChange.bind(this);
}
componentDidMount() {
    userStore.addListener(ViewUpdateTypes.USER_UPDATE, this._onChange);
    userStore.addListener(ViewUpdateTypes.FD_MENU_UPDATE, this._onChange);
}
componentWillUnmount() {
    userStore.removeListener(ViewUpdateTypes.USER_UPDATE, this._onChange);
    userStore.removeListener(ViewUpdateTypes.FD_MENU_UPDATE, this._onChange);
}

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