无法从异步存储中检索密钥 反应本机



我正在尝试使用"记住我"切换开关将用户电子邮件存储在异步存储中。我不确定我为什么要检索";空";使用我的getRememberedUser调用?我做错了什么?这是React Native,我也有一个redux存储(这里没有真正使用(,但不确定这是否重要。谢谢

const [userEmail, setuserEmail] = useState("");
const [rememberMe, setrememberMe] = useState(false);
const rememberUser = async () => {
try {
await AsyncStorage.setItem("userEmail", userEmail);
console.log("stored");
} catch (error) {
// Error saving data
}
};
const forgetUser = async () => {
try {
await AsyncStorage.removeItem("userEmail");
console.log("forgotten");
} catch (error) {
// Error removing
}
};
const getRememberedUser = async () => {
try {
const value = await AsyncStorage.getItem("userEmail");
console.log(value)
if (value !== null) {
// We have username!!
setuserEmail(value);
}
} catch (error) {
// Error retrieving data
}
};
<Input
id="email"
label="E-Mail"
keyboardType="email-address"
required
email
autoCapitalize="none"
errorText="Please enter a valid email address."
onInputChange={inputChangeHandler}
initialValue={userEmail}
/>
<Switch
value={rememberMe}
onValueChange={(value) => toggleRememberMe(value)}
/>
<Text>Remember Me</Text>
<Button
title="get uersname"
onPress={() => {
getRememberedUser();
}}
></Button>

保存时需要字符串,

await AsyncStorage.setItem("userEmail", JSON.stringify(userEmail));

并在检索时进行解析

const value = await AsyncStorage.getItem("userEmail");
const userEmail = JSON.parse(value);

相关内容

  • 没有找到相关文章

最新更新