贴个官网例子,使用的时候再细看
//fetch 网络请求
import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, Text, View } from 'react-native';const App = () => {
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);useEffect(() => {
fetch('https://reactnative.dev/movies.json')
.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.title}, {item.releaseYear}</Text>
)}
/>
)}
</View>
);
};
export default App