Lui*_*rgo 6 react-native asyncstorage
我试图使用异步存储来进行用户身份验证,以决定将呈现哪个屏幕,所以当我从异步存储中获取数据时返回给我未定义有人可以帮我吗?
我的获取代码:
var login;
AsyncStorage.getItem("login").then((value) => {
login = value;
}).done();
alert(login);
Run Code Online (Sandbox Code Playgroud)
我的设定代码:
const insert_user = (username) => {
AsyncStorage.setItem("login", username);
Toast.show({
supportedOrientations: ['portrait', 'landscape'],
text: `User registered with success`,
position: 'bottom',
buttonText: 'Dismiss'
});
}
Run Code Online (Sandbox Code Playgroud)
Sha*_*osa 12
alert(login); 将始终未定义,因为AsyncStorage.getItem本质上是异步的意味着在从AsyncStorage.getItem接收值之前首先调用alert(login)
AsyncStorage.getItem("login").then((value) => {
alert(value); // you will need use the alert in here because this is the point in the execution which you receive the value from getItem.
// you could do your authentication and routing logic here but I suggest that you place them in another function and just pass the function as seen in the example below.
});
// a function that receives value
const receiveLoginDetails = (value) => {
alert(value);
}
// pass the function that receives the login details;
AsyncStorage.getItem("login").then(receiveLoginDetails);
Run Code Online (Sandbox Code Playgroud)
进一步参考
| 归档时间: |
|
| 查看次数: |
9626 次 |
| 最近记录: |