Как отобразить данные из AsyncStorage?
Всем привет. У меня мобильное приложение сохраняет json с сервера, но при попытке отобразить данные функция возвращает мне пустой массив.
const [data, setData] = useState([]);
const getData = async (key) => {
try {
const jsonValue = await AsyncStorage.getItem(key);
if (jsonValue !== null) {
setData(JSON.parse(jsonValue));
console.log(JSON.parse(jsonValue));
} else {
console.log('reading err');
}
} catch(e) {
// error reading value
console.log('reading err');
}
};
const storeData = async (key, value) => {
try {
await AsyncStorage.setItem(key, value);
console.log('sexy save')
} catch (e) {
// saving error
console.log('saving error')
}
};
const postToServer = async (img) => {
const formData = new FormData();
formData.append('image', {
uri: img.uri,
type: 'image/jpeg',
name: 'test.jpg',
})
formData.append('title', 'title');
let res = await fetch('http://192.168.0.149:8000/api/textocr/', { //http://192.168.0.149:8000/api/textocr/ HomeNetwork, http://172.20.10.2:8000/api/textocr/ MobileNetwork
method: 'POST',
body: formData,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
});
let json = await res.json(); // Get JSON Object from Django
json = JSON.stringify(json.result); // Stringify this JSON to storeData func
let resultjson = json; // This const is need to alert result on display
resultjson = JSON.parse(resultjson); // Parse result
Alert.alert(t('resultRecongnize'), resultjson);
const update = ([...data, {id: data.length, name: resultjson .substring(0, 15), content: resultjson }]); //{id: data.length, name: json.substring(0, 15), content: json }
setData(update);
storeData('key', JSON.stringify(data));
};
useEffect(() => {
getData('key');
}, []);