简体   繁体   English

如何在ReactJS ES6中删除Listener

[英]How to removeListener in reactjs es6

I use es6 in project of react. 我在React项目中使用es6。

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. 现在我想要removeListener,该怎么办。

You can do constructor and Function.prototype.bind like this. 您可以像这样执行constructorFunction.prototype.bind

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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