I am using a event emitter to communicate between a map component and the toolbar. Note* I am using this same code in other parts of my app without issue. The error I am getting is :
我正在使用事件发射器在映射组件和工具栏之间进行通信。注意*我在我的应用程序的其他部分使用同样的代码,没有问题。我得到的错误是:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
警告:setState(…):只能更新已安装或正在安装的组件。这通常意味着在未挂载的组件上调用setState()。这是一个空操作。请检查未定义组件的代码。
I have tried to solve this by similar posts but it is not working. I thought it had to do with the mount && unmount methods in both components?
我曾试图通过类似的帖子来解决这个问题,但没有成功。我认为这与两个组件中的挂载和卸载方法有关?
Toolbar Component
工具栏组件
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
})
}
Map component
映射组件
onMarkerPress(val) {
AppEventEmitter.emit('showSubdivisionIcon', val.id);
}
The console error detail for the EventEmitter.js leads to this
EventEmitter的控制台错误细节。js导致这
subscription.listener.apply(
subscription.context,
Array.prototype.slice.call(arguments, 1)
);
Complete section in EventEmitter.js
完成部分EventEmitter.js
/**
* Emits an event of the given type with the given data. All handlers of that
* particular type will be notified.
*
* @param {string} eventType - Name of the event to emit
* @param {...*} Arbitrary arguments to be passed to each registered listener
*
* @example
* emitter.addListener('someEvent', function(message) {
* console.log(message);
* });
*
* emitter.emit('someEvent', 'abc'); // logs 'abc'
*/
emit(eventType: String) {
var subscriptions = this._subscriber.getSubscriptionsForType(eventType);
if (subscriptions) {
var keys = Object.keys(subscriptions);
for (var ii = 0; ii < keys.length; ii++) {
var key = keys[ii];
var subscription = subscriptions[key];
// The subscription may have been removed during this event loop.
if (subscription) {
this._currentSubscription = subscription;
subscription.listener.apply(
subscription.context,
Array.prototype.slice.call(arguments, 1)
);
}
}
this._currentSubscription = null;
}
}
1 个解决方案
#1
6
The only problem is that your event listeners are not removed, because the name of the componentWillUnmount
method is incorrect. In your code the M
of mount
is capital, where as it should be lowercase.
唯一的问题是没有删除事件侦听器,因为componentWillUnmount方法的名称不正确。在代码中,mount的M是大写的,应该是小写的。
componentWillUnmount() {
this.showLocateIconListener.remove();
this.endTripListener.remove();
this.endSubdivisionIcon.remove();
}
#1
6
The only problem is that your event listeners are not removed, because the name of the componentWillUnmount
method is incorrect. In your code the M
of mount
is capital, where as it should be lowercase.
唯一的问题是没有删除事件侦听器,因为componentWillUnmount方法的名称不正确。在代码中,mount的M是大写的,应该是小写的。
componentWillUnmount() {
this.showLocateIconListener.remove();
this.endTripListener.remove();
this.endSubdivisionIcon.remove();
}