[英]How to removeListener in reactjs es6
我在React項目中使用es6。
componentDidMount() {
userStore.addListener(ViewUpdateTypes.USER_UPDATE,(data)=>this._onChange(data));
userStore.addListener(ViewUpdateTypes.FD_MENU_UPDATE,(data)=>this._onChange(data));
};
現在我想要removeListener,該怎么辦。
您可以像這樣執行constructor
和Function.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.