导入:
注意: 新版本的React Native不再集成async-storage
npm install @react-native-async-storage /async-storage
yarn add @react-native-async-storage /async-storage
使用:
注意: getItem必须配合async与await使用否则将会显示{"_h": 0, "_i": 0, "_j": null, "_k": null}
1 import AsyncStorage from '@react-native-async-storage/async-storage'; 2 3 export const SaveToken = value => { 4 AsyncStorage.setItem('token_key', value); 5 }; 6 7 export const GetToken = async () => { 8 value = await AsyncStorage.getItem('token_key'); 9 return value; 10 };
组件取值:
1 try { 2 const token = await GetToken(); 3 console.log('token', token); 4 } catch (error) { 5 console.log('error', error); 6 }
官方文档: https://react-native-async-storage.github.io/async-storage
标签:react,存储,storage,value,React,token,async,native,Native From: https://www.cnblogs.com/M1NG/p/17148125.html