React Native 中如何使用 AsyncStorage 存储数组
How to use AsyncStorage to store array in React Native
我正在尝试使用 AsyncStorage 来存储使用 React Native 的简单配置文件。
这是我到目前为止添加配置文件的内容,我尝试了 console.log(联系人),但没有显示任何内容。
function AddContact(props) {
const [FName,setFName] = useState('');
const [LName,setLName] = useState('');
const [PNumber,setPNumber] = useState('');
var contact = {
firstname : FName,
lastname : LName,
phonenumber : PNumber
};
const storeContact = async() => {
try {
await AsyncStorage.setItem(
'@MySuperStore:key', JSON.stringify(contact));
} catch (error) {
// Error saving data
}}
For Loading Profile
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key', JSON.stringify(contact));
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};
请帮忙。
AsyncStorage.getItem()
只接受一个关键参数。所以你必须只传递参数 "key" 并且它将 return 你的数据作为字符串:
const value = await AsyncStorage.getItem('@MySuperStore:key');
或者,如果您想使用此 value
作为数组来显示某些内容,您必须将其转换为 json。
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
if (value !== null) {
// We have data!!
console.log(value);
this.setState({
data: JSON.parse(value);
});
}
} catch (error) {
// Error retrieving data
}
};
为了从异步存储加载数据,您的 loadContact 方法应该是:
For Loading Profile
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
const json = JSON.parse(value); // this is how you get back the array stored
if (json !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};
我正在尝试使用 AsyncStorage 来存储使用 React Native 的简单配置文件。
这是我到目前为止添加配置文件的内容,我尝试了 console.log(联系人),但没有显示任何内容。
function AddContact(props) {
const [FName,setFName] = useState('');
const [LName,setLName] = useState('');
const [PNumber,setPNumber] = useState('');
var contact = {
firstname : FName,
lastname : LName,
phonenumber : PNumber
};
const storeContact = async() => {
try {
await AsyncStorage.setItem(
'@MySuperStore:key', JSON.stringify(contact));
} catch (error) {
// Error saving data
}}
For Loading Profile
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key', JSON.stringify(contact));
if (value !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};
请帮忙。
AsyncStorage.getItem()
只接受一个关键参数。所以你必须只传递参数 "key" 并且它将 return 你的数据作为字符串:
const value = await AsyncStorage.getItem('@MySuperStore:key');
或者,如果您想使用此 value
作为数组来显示某些内容,您必须将其转换为 json。
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
if (value !== null) {
// We have data!!
console.log(value);
this.setState({
data: JSON.parse(value);
});
}
} catch (error) {
// Error retrieving data
}
};
为了从异步存储加载数据,您的 loadContact 方法应该是:
For Loading Profile
const loadContact = async () => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
const json = JSON.parse(value); // this is how you get back the array stored
if (json !== null) {
// We have data!!
console.log(value);
}
} catch (error) {
// Error retrieving data
}
};