Navigation - React Navigation 'focus' listener is not triggered the first time
See original GitHub issueDescribe the bug
If I wrap the screen with register()
method the React Navigation 5 ‘focus’ listener is not triggered the first time. This happens also with React Component or PureComponent not just with Functional Component.
Code snippet
...
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
// The screen is focused
// Call any action
console.log("FOCUS DETAILS"); // Not triggered the first time
});
// Return the function to unsubscribe from the event so it gets removed on unmount
return unsubscribe;
}, [navigation]);
...
Repo for reproducing Here you can find an example repository --> https://github.com/alessioemireni/react-native-bundle-splitter-example. It’s a fork of your example repository.
To Reproduce Steps to reproduce the behavior:
- Open the App
- See the console log. The ‘Focus Home’ is printed.
- Click on ‘Go to details’
- See the console log. The ‘Focus Details’ log is not printed.
- Go Back.
- See the console log. The ‘Focus Home’ is printed.
- Click on ‘Go to details’
- See the console log. The ‘Focus Details’ log is now printed.
Expected behavior The React Navigation 5 ‘focus’ listener should be triggered.
Smartphone (please complete the following information):
- Desktop OS: [MacOS 10.15.7]
- Device: [iPhone XR, Google Pixel 2 XL]
- OS: [iOS 14.2, Android 11]
- JS enginge [JSC, Hermes]
- Library version [2.0.0]
- React Native: [0.62.2]
- React Navigation: [5.9.2]
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Call a function when focused screen changes | React Navigation
Call a function when focused screen changes · Listening to the 'focus' event with an event listener. · Using the useFocusEffect hook provided...
Read more >'focus' listener not calling every time. React Native
It is calling once at first time, not working second time. I have used following code for execute 'focus' listener on tab screen...
Read more >didFocus doesn't get triggered at first load when using tab ...
It works fine when I navigate to a stacknavigator. componentDidMount() { this.listener = this.props.navigation.addListener('didFocus' ...
Read more >Lifecycle hooks are not enough with React Navigation in ...
When the Profile screen is unmounted, we should stop listening to events. That is why we remove the event listener by returning the...
Read more >Events | React Native Navigation
Used to set the initial layout of the Application - after that the app is ready for user interaction. const appLaunchedListener = Navigation.events() ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @alessioemireni Thanks for the detailed issue! I will have a look on that.
We decided to use an Hoc with useFocusEffect react navigation Hook due to the fact that that all Screens in main project use focus Listener. I’ll close the issue.