[英]REACT NATIVE : How to fetch API data (JSON) and insert into Flatlist, API
Hello everyone i got API data from https://dog.ceo/api/breeds/list/all -list of all breeds, and inserted already into FlatList, now i want to insert their picture from: https://dog.ceo/api/breed/${breed}/images , but can't solve how to solve this problem the FlatList
breed of dogs it should have its own picture(, maybe any tips for the freshman?大家好,我从https://dog.ceo/api/breeds/list/all获得了 API 数据:所有品种的列表,并且已经插入到 FlatList 中,现在我想从以下位置插入他们的图片:Z5E056C500A1C4B7BADE5Z11。 /api/breed/${breed}/images ,但无法解决如何解决这个问题FlatList
品种的狗它应该有自己的图片(也许对大一新生有什么建议?
//Here i am geting data to list all breeds:
useEffect(() => {
fetch('https://dog.ceo/api/breeds/list/all')
.then((response) => response.json())
.then((json) => setData(Object.keys(json.message)))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, []);
<FlatList
data={data}
keyExtractor={({ id }, index) => index.toString()}
renderItem={({ item }) => (
<Text>{item} </Text>
<Image
style={styles.tinyLogo}
source={{
uri: //here i have wanted to put url of JSON, but i should first fetch it , but i dont know how to fetch the image that each breed should have its own PICTURE,
)
}}
/>
The basic idea I use is to use a state variable for我使用的基本思想是使用 state 变量
data={this.state.yourListData}
that will get a value (list) using a cmd like setState to be executed from within the promise you use to fetch the data.这将使用 cmd (如 setState)获得一个值(列表),从用于获取数据的 promise 中执行。 As I understand you want to show a list of components each having a picture.据我了解,您想显示一个组件列表,每个组件都有一张图片。 If this is the case I like to create a React component that includes an Image (or similar) component.如果是这种情况,我喜欢创建一个包含 Image(或类似)组件的 React 组件。 Lets call this component MyCard.让我们将此组件称为 MyCard。 Then, at the renderItem I use something like:然后,在 renderItem 我使用类似的东西:
renderItem={({ item }) => ( < MyCard item={item} /> ) }
References:参考:
https://reactnative.dev/docs/flatlist
https://reactnative.dev/docs/images
Make component for ListItem
:为ListItem
制作组件:
ListItem.js ListItem.js
import React, {useCallback, useEffect} from 'react';
import PropTypes from 'prop-types';
import {Text, View, ActivityIndicator} from 'react-native';
const ListItem = ({breed}) => {
const [image, setImage] = useState('');
const getImage = useCallback(async () => {
const resp = await fetch(`https://dog.ceo/api/breed/${breed}/images/random`);
const data = await resp.json();
setImage(data.message);
}, [breed]);
useEffect(() => {
getImage();
}, [getImage]);
return (
<View>
<Text>{breed}</Text>
{ image ? (
<Image source={{uri: image}} />
) : (
<ActivityIndicatior />
) }
</View>
);
};
ListItem.propTypes = {
breed: PropTypes.string;
};
export default ListItem;
FlatList平面列表
import ListItem from './ListItem';
<FlatList
data={data}
keyExtractor={({ id }, index) => index.toString()}
renderItem={
({item}) => <ListItem breed={item} />
}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.