如何在不按反应本机的情况下从异步存储中获取数据

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

我想AsyncStorage在没有onPress的情况下将数据保存在变量中,在打开的应用程序屏幕上显示数据

代码:

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

您必须使用 React 钩子,特别是 useEffect 钩子在应用启动时调用函数

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

useEffect(() => { appScreen ();}, []);