ija*_*cha 3 javascript reactjs react-native asyncstorage
async function getTokenFromAsync() {
const userToken = await AsyncStorage.getItem('@User_Token');
return userToken;
}
export default {getTokenFromAsync};
Run Code Online (Sandbox Code Playgroud)
我试图从 asyncStorage 获取 userToken 但它返回给我这个 {"_U": 0, "_V": 0, "_W": null, "_X": null},我使用 React Native
您没有解决 Promise,因此您的输出“奇怪”。
这是我定义 get 函数的方式:
const getData = async (key) => {
// get Data from Storage
try {
const data = await AsyncStorage.getItem(key);
if (data !== null) {
console.log(data);
return data;
}
} catch (error) {
console.log(error);
}
};
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过使用密钥调用函数来获取数据。
await getData("yourKey")
.then(data => data)
.then(value => {
a state or constant = value
console.log("yourKey Value: " + value)
})
.catch(err => console.log(err))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4950 次 |
| 最近记录: |