In my React Native app I have two screens screenA
and screenB
. I'm using react-navigation@4.x
and I navigate from screenA
to screenB
with this.props.navigation.navigate('screenB')
and back with this.props.navigation.goBack()
.
I'm using OneSignal
, and when screenA
mounts I add a OneSignal event listener.
screenA
OneSignal.addEventListener('opened', () => console.log(this.props.navigation.state.routeName));
I have the same code in screenB
, and up until this point, it works as expected. When I'm on
screenA
and open a push notification, it logs screenA
, and when I'm on screenB
it logs screenB
, because screenB
's event listener overrides screenA
's.
However, when I navigate back to screenA
, screenB
's event listener is still in place, so I add this code to screenB
to remove its event listener:
screenB
componentWillUnmount() {
OneSignal.removeEventListener('opened');
}
and to get screenA
to re-invoke its event listener, I add this code:
screenA
componentDidUpdate(prevProps) {
if (!prevProps.isFocused && this.props.isFocused) {
OneSignal.addEventListener('opened', () => console.log(this.props.navigation.state.routeName));
}
}
**The Problem: **
This isFocused
condition is triggered when I return to screenA
, so the addEventListener
code block is reached, but the event listener isn't successfully added.
Does anyone know how I can approach troubleshooting this?