Reputation: 3855
I'm adding a new listener to BackAndroid on:
componentWillMount () {
BackAndroid.addEventListener('hardwareBackPress', backAndroidHandler.bind(this));
}
and I'm removing the listener on:
componentWillUnmount () {
BackAndroid.removeEventListener('hardwareBackPress', backAndroidHandler);
}
function backAndroidHandler () {
...
}
but when I unmount the component and mount it again, there are two of the same function and the same event fires twice. Any clue?
Upvotes: 1
Views: 2489
Reputation: 483
this.androidBackHandler = this.backAndroidHandler.bind(this);
It should be like this.
Upvotes: 1
Reputation: 3855
Fixed it
componentWillMount () {
this.androidBackHandler = backAndroidHandler.bind(this);
BackAndroid.addEventListener('hardwareBackPress', this.backAndroidHandler);
}
componentWillUnmount () {
BackAndroid.removeEventListener('hardwareBackPress', this.androidBackHandler);
}
function backAndroidHandler () {
...
}
I tried declaring the function directly on the component but this
was not binding, it was undefined
Upvotes: 3