You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 3, 2022. It is now read-only.
I just want to warn that this implementation is problematic:
exportfunctionuseNavigationEvents(handleEvt){constnavigation=useNavigation();useEffect(()=>{constsubsA=navigation.addListener('action',handleEvt);constsubsWF=navigation.addListener('willFocus',handleEvt);constsubsDF=navigation.addListener('didFocus',handleEvt);constsubsWB=navigation.addListener('willBlur',handleEvt);constsubsDB=navigation.addListener('didBlur',handleEvt);return()=>{subsA.remove();subsWF.remove();subsDF.remove();subsWB.remove();subsDB.remove();};},// For TODO consideration: If the events are tied to the navigation object and the key// identifies the nav object, then we should probably pass [navigation.state.key] here, to// make sure react doesn't needlessly detach and re-attach this effect. In practice this// seems to cause troublesundefined// [navigation.state.key]);}
The key is needed, otherwise the events will be removed/added on every update.
Unfortunately when you add a didFocus listener on a focused view, the listener will fire immediately after adding (async), which has lead to an infinite loop on this issue: react-navigation/react-navigation#5058
I suggest as a solution to:
store react-navigation listeners in a ref to make them stable
wire the user provided listeners to the stable listeners of the ref (so that user is still able to provide arrow functions/unstable listeners, and the latest provided version will be called, instead of the one provided on initial render)
only update react-navigation listeners on some specific conditions, like key change?
I can try to provide an implementation if you want