Answer the question
In order to leave comments, you need to log in
React Native - JSON. How to get value via API not as a list?
React Native (17.0.2) clean, no axios etc., trying to do it with fetch.
The JSON itself is an example here - https://itstav.ru/1.json
{
"response":"success",
"value":{
"name":"test",
"text":"test"
},
"list": [
{ "id": "1", "title": "test", "releaseYear": "1977" },
{ "id": "2", "title": "test", "releaseYear": "1977" }
]
}
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, Text, View } from 'react-native';
export default App = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
const getMovies = async () => {
try {
const response = await fetch('https://itstav.ru/1.json');
const json = await response.json();
setData(json.movies);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
}
useEffect(() => {
getMovies();
}, []);
return (
<View style={{ flex: 1, padding: 24 }}>
{/* Тут нужно отобразить value->name */}
{isLoading ? <ActivityIndicator/> : (
<FlatList
data={data}
keyExtractor={({ id }, index) => id}
renderItem={({ item }) => (
<Text>{item.title}, {item.releaseYear}</Text>
)}
/>
)}
</View>
);
};
name
from the object value
right after <View style={{ flex: 1, padding: 24 }}>
once before the entire list (i.e. display test
)?
Answer the question
In order to leave comments, you need to log in
setData(json.movies);
Rename setData to setMovies and add a state for value similar to it. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question