I am working on mobile app with React Native and have discovered this weird behaviour using EventEmitter in my project. I am using Flux. In my Scene (Component) I listen to Event as seen below in code:
Component:
componentWillMount(){
BasketStore.addChangeListener(this._onChange.bind(this));
}
componentWillUnmount(){
BasketStore.removeChangeListener(this._onChange.bind(this));
}
BaseStore:
export default class BaseStore extends EventEmitter {
constructor() {
super();
}
emitChange() {
this.emit(CHANGE_EVENT);
}
addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
}
removeChangeListener(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
subscribe(actionSubscribe) {
this._dispatchToken = AppDispatcher.register(actionSubscribe());
}
get dispatchToken() {
return this._dispatchToken;
}
}
BaskekStore:
class BasketStore extends BaseStore {...}
In Navigator I do navigator.pop()
to go back in my route. From documentation it says that it should unmount my Component every time I do pop, which I use to go back in my previous scene. However it does not, and my Component still listens to _onChange
event which make app behave wrong, the more I swap between views app becomes slow and eventually crush as there are too many views and listeners.
Thanks in advance!