我是新手,我想把我的数据从securerestore传递到props。但我现在不知道如何正确处理promise函数并提取它的数据
当我像这样传递数据时,我的对象是未定义的。
这是我的密码。
const Authenticate = () => {
//const [deviceId, setDeviceId] = useState("");
//SecureStore.deleteItemAsync("device_id").then((reject) => {
//});
//const resp = {code : 3, expositions : 5}
SecureStore.getItemAsync("device_id").then((response) => {
//setDeviceId({"id" : response});
console.log("stored id : " + response);
//let resp = sendId({'id' : response})
test(response);
}
);
function test (info){
return info;
}
return (
<Expositions infections={test()}/>
);
};
export default Authenticate;
你真的应该阅读这个
const Authenticate = () => {
const [myStuff, setMyStuff] = useState();
//this will be executed on mount
useEffect(()=>{
SecureStore.getItemAsync("device_id").then((response) => {
setMyStuff(response)
}
);
},[])
console.log(myStuff)
}
在第一次渲染时,myStuff
是undefined
,然后在获取数据后,setMyStuff
将触发一个新的渲染,您将看到更新的数据
使用react state hook存储promise提供的数据,然后将状态传递给渲染的jsx。