我想知道您是否可以帮我弄清楚为什么当我使用异步存储在 React Native 中重新加载我的应用程序时我的数据没有保留。这是我的详细信息:
React Native .61.2/使用 React Native 社区 – 在 iOS 上的 XCode 模拟器中进行异步存储/测试
当我最初设置我的数据然后获取它时,我得到了正确的响应。当我设置我的数据、重新加载应用程序并尝试再次获取我的数据时,它以字母“a”的形式返回。
这是我设置数据的方式:
changeUsernameToLowercase = text => {
var temp = text.toLowerCase();
this.setState({email: temp});
this._storeData('@email', temp);
};
async _storeData(name, value) {
try {
await AsyncStorage.setItem(name, value);
} catch (error) {
// Error saving data
console.log('FAILING: ' + error);
}
}
这是我的渲染中调用 changeUsernameToLowercase 的 JSX:
<TextInput
onChangeText={text => this.changeUsernameToLowercase(text)}
placeholder="Email address"
value={this.state.email}
autoCapitalize="none"
type="password"
style={{
flex: 1,
backgroundColor: '#fff',
borderBottomColor: '#333',
borderBottomWidth: 1,
}}
/>
这是我获取数据的方式:
componentDidMount() {
this._retrieveEmail().then(response => {
console.log('My log:' + response); //returns letter a on reload
return response;
});
}
async _retrieveEmail() {
try {
var email = await AsyncStorage.getItem('@email');
console.log('MY EMAIL LOG:' + email); //returns letter a on reload
return email;
} catch (e) {
console.error(e);
}
}
我可能误解了持久存储的工作原理,但我认为这是为了让数据在重新加载之类的情况下持久存在?
感谢任何帮助!
最佳答案
在我的 iOS React Native 应用中,AsyncStorage 没有在刷新时持久保存,因为我还使用了 Firebase 身份验证。
Firebase 身份验证在后台使用来自 react-native
的 AsyncStorage。如果您还使用 @react-native-async-storage/async-storage
,这会造成冲突。
问题已记录 here .
关于react-native - 异步存储不会在应用程序重新加载时持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58599213/