Javascript 如何解决react本机EventEmitterListener警告
我使用事件发射器在贴图组件和工具栏之间进行通信。注意*我在我的应用程序的其他部分使用相同的代码,没有问题。我得到的错误是: 警告:设置状态(…):只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是禁止操作。请检查未定义组件的代码 我曾试图通过类似的帖子来解决这个问题,但它不起作用。我认为这与这两个组件中的mount&&unmount方法有关 工具栏组件Javascript 如何解决react本机EventEmitterListener警告,javascript,node.js,reactjs,react-native,eventemitter,Javascript,Node.js,Reactjs,React Native,Eventemitter,我使用事件发射器在贴图组件和工具栏之间进行通信。注意*我在我的应用程序的其他部分使用相同的代码,没有问题。我得到的错误是: 警告:设置状态(…):只能更新已安装或正在安装的组件。这通常意味着您在未安装的组件上调用了setState()。这是禁止操作。请检查未定义组件的代码 我曾试图通过类似的帖子来解决这个问题,但它不起作用。我认为这与这两个组件中的mount&&unmount方法有关 工具栏组件 componentDidMount() { this.showLocateI
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的控制台错误详细信息导致了这种情况
subscription.listener.apply(
subscription.context,
Array.prototype.slice.call(arguments, 1)
);
完成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;
}
}
/**
*发出具有给定数据的给定类型的事件。所有的处理者
*将通知特定类型。
*
*@param{string}eventType-要发出的事件的名称
*@param{…*}要传递给每个已注册侦听器的任意参数
*
*@example
*emitter.addListener('someEvent',函数(消息){
*控制台日志(消息);
* });
*
*emit('someEvent','abc');//日志“abc”
*/
发出(事件类型:字符串){
var subscriptions=this.\u subscriber.getSubscriptionsForType(eventType);
if(订阅){
var keys=Object.keys(订阅);
对于(变量ii=0;ii
唯一的问题是没有删除事件侦听器,因为组件willunmount
方法的名称不正确。在您的代码中,mount
的M
是大写,其中as应该是小写
componentWillUnmount() {
this.showLocateIconListener.remove();
this.endTripListener.remove();
this.endSubdivisionIcon.remove();
}