嗨,我似乎无法解决这个问题——我在React Native组件中使用AsyncStorage来加载文件路径字符串。当组件安装时,我收到了以下错误,并且没有看到我在这里做得不正确,因为它几乎遵循了文档中的示例。任何帮助都将不胜感激。
"TypeError:undefined不是对象(正在评估"_asyncStorage.asyncStorage.getKeys"(
这是我的代码:
import React, { Component } from 'react';
import BasicCard from './cards'
import {AsyncStorage} from '@react-native-community/async-storage'
import {
StyleSheet,
ScrollView,
} from 'react-native';
class NewPost extends Component{
state={
content:[]
}
componentDidMount(){
this.getContent(this.getKeys())
}
getKeys = async () => {
try{
return await AsyncStorage.getKeys()
}catch (e) {
alert(e)
}
}
getContent = async (keys) => {
try{
await AsyncStorage.multiGet(keys, store)
this.setState({content:store})
} catch (e) {
alert ( e )
}
}
render(){
return(
<ScrollView style={styles.container}>
{this.state.content.map((result,i,item) => (
<BasicCard
imgName={item[i][0]}
imgPath={item[i[1]]} />
))}
</ScrollView>
)
}
}
export default NewPost;
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: 'black',
},
});
asyncStorage 中没有名为getkeys的函数
请参阅此链接了解异步存储中的可用功能
getKeys = async () => {
try{
return await AsyncStorage.getKeys()
}catch (e) {
alert(e)
}
}
更改为该
getKeys = async () => {
try{
return await AsyncStorage.getAllKeys()
}catch (e) {
alert(e)
}
}