react-native – 异步存储无法设置或启动React Native
发布时间:2020-12-15 05:05:38 所属栏目:百科 来源:网络整理
导读:我试图使用异步存储来进行用户身份验证,以决定将呈现哪个屏幕,所以当我从异步存储中获取数据时返回给我未定义有人可以帮我吗? 我的获取代码: var login;AsyncStorage.getItem("login").then((value) = { login = value;}).done();alert(login); 我的设定代
我试图使用异步存储来进行用户身份验证,以决定将呈现哪个屏幕,所以当我从异步存储中获取数据时返回给我未定义有人可以帮我吗?
我的获取代码: var login; AsyncStorage.getItem("login").then((value) => { login = value; }).done(); alert(login); 我的设定代码: const insert_user = (username) => { AsyncStorage.setItem("login",username); Toast.show({ supportedOrientations: ['portrait','landscape'],text: `User registered with success`,position: 'bottom',buttonText: 'Dismiss' }); }
警报(登录);将始终未定义,因为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); 进一步参考 > AsyncStorage.getItem将返回Promise:https://www.promisejs.org/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |