简体   繁体   中英

React Native : Trying to fetch API and add to Flatlist

trying to fethch data and insert to Flatlist, can someone show my mistake, tried almost everything, Tried to make like in Official Docs , but didnt help:

const [isLoading, setLoading] = useState(true);
  const [data, setData] = useState([]);
  
    useEffect(() => {
      fetch('https://app.sm117.ru/api/v1/contract/news/'),{
        method: "GET",
        headers: {"Content-type": "application/json; charset=UTF-8"}}

        .then((response) => response.json())
        .then((json) => setData(json.movies))
        .catch((error) => console.error(error))
        .finally(() => setLoading(false));
      }, []);
  
    return (
      <View style={{ flex: 1, padding: 24 }}>
        {isLoading ? <ActivityIndicator/> : (
          <FlatList
            data={data}
            keyExtractor={({ id }, index) => id}
            renderItem={({ item }) => (
              <Text>{item.id}, {item.title}</Text>
            )}
          />
        )}
      </View>
    );
  };

Why my code does not work???

Im not sure the file you are trying to get is JSON file

At least the last parentheses is missing after

headers: {"Content-type": "application/json; charset=UTF-8"}}

There should be a “)” to close the “fetch” call before “then”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM