2016-05-19 30 views
10

我正在使用事件发射器在地图组件和工具栏之间进行通信。注*我在我的应用程序的其他部分使用相同的代码没有问题。我得到的错误是:如何解决反应原生EventEmitterListener警告

警告:setState(...):只能更新挂载或挂载组件。这通常意味着您在卸载的组件上调用了setState()。这是一个没有操作。请检查未定义组件的代码。

我试图通过类似的帖子解决这个问题,但它不工作。我认为它必须与两个组件的卸载方法中的安装& &?

工具栏组件

 componentDidMount() { 
    this.showLocateIconListener = AppEventEmitter.addListener('isTrip', this.isTrip.bind(this)); 
    this.endTripListener = AppEventEmitter.addListener('showLocateIcon', this.showLocateIcon.bind(this)); 
    this.endSubdivisionIcon = AppEventEmitter.addListener('showSubdivisionIcon', this.showSubdivisionIcon.bind(this)); 
} 

componentWillUnMount() { 
    this.showLocateIconListener.remove(); 
    this.endTripListener.remove(); 
    this.endSubdivisionIcon.remove(); 
} 


//// this is where the error is happening 
showSubdivisionIcon(val) { 
    if (val != 0) 
     this.setState({ 
      items: menuSubdivision, 
      subdivisionId: val 
     }) 
    else 
     this.setState({ 
      items: menu 
     }) 
} 

地图组件

onMarkerPress(val) { 
    AppEventEmitter.emit('showSubdivisionIcon', val.id); 
} 

为EventEmitter.js控制台错误细节导致EventEmitter.js这个

subscription.listener.apply(
     subscription.context, 
     Array.prototype.slice.call(arguments, 1) 
    ); 

完成部分

回答

6

唯一的问题是您的事件侦听器未被删除,因为componentWillUnmount方法的名称不正确。在你的代码中,的mount是大写字母,因为它应该是小写字母。

componentWillUnmount() { 
    this.showLocateIconListener.remove(); 
    this.endTripListener.remove(); 
    this.endSubdivisionIcon.remove(); 
}