Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native How to execute function every time when i open page

Tags:

react-native

I need to send request every time when i open page. Currently when i access page first time after load the app everything is ok, but if i go to another page and back after that request is not send it again.

like image 665
Любомир Мариянов Avatar asked Sep 19 '25 06:09

Любомир Мариянов


1 Answers

You have to add focus listener so when you go back, It will refresh the data like

import * as React from 'react';
import { View } from 'react-native';

function AppScreen({ navigation }) {
  React.useEffect(() => {
    const unsubscribe = navigation.addListener('focus', () => {
      // The screen is focused
      // Call any action and update data
    });

    // Return the function to unsubscribe from the event so it gets removed on unmount
    return unsubscribe;
  }, [navigation]);

  return <View />;
}

source : https://reactnavigation.org/docs/function-after-focusing-screen/

like image 115
Nooruddin Lakhani Avatar answered Sep 21 '25 11:09

Nooruddin Lakhani