Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

event when component is mounted before render React Native

I’m creating an React Native App with tab and stack navigation. Is there any solution to handle when its just navigated before render?

For example:

Chats.jsx
...
<Pressable
    onPress={() => props.navigate('Messages', { chatId: chat.id })}
                        ><Chats/>
                        </Pressable>
...

I click the button and jump to MessagesScreen.jsx screen.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

MessagesScreen.jsx
...
//Some logic after navigation event. For example, I want to fetch data from a remote source and show at the screen.
...

>Solution :

Use focus event, something like this:

function MessagesScreen({ navigation }) {
  React.useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
      // TODO
    });

    return unsubscribe;
  }, [navigation]);

  return <... />;
}

See React Navigation lifecycle events for details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading