question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Navigation - React Navigation 'focus' listener is not triggered the first time

See original GitHub issue

Describe 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:

  1. Open the App
  2. See the console log. The ‘Focus Home’ is printed.
  3. Click on ‘Go to details’
  4. See the console log. The ‘Focus Details’ log is not printed.
  5. Go Back.
  6. See the console log. The ‘Focus Home’ is printed.
  7. Click on ‘Go to details’
  8. 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kirillzyuskocommented, Apr 13, 2021

Hi @alessioemireni Thanks for the detailed issue! I will have a look on that.

1reaction
alessioemirenicommented, Apr 16, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found