Search code examples
react-nativeasyncstorage

how to get data from asyncstorage without on press in react native


I want to AsyncStorage save data in variable without onPress , show data on open app screen

code:

const AppScreen = () => {
   var getusername = async () => {
      await AsyncStorage.getItem('name');
   };
   return (
      <Text>{getusername}</Text>
   );
};
export default AppScreen;

Solution

  • You have to use react hooks, in particular, useEffect hook to call the function as the app launches

    const appScreen = () => { var getusername = async () => { await AsyncStorage.getItem('token'); };};
    
    useEffect(() => { appScreen ();}, []);