如何在flatlist中显示不同的数据集。反应天然
我有两个不同的数据集,我将添加到不同的状态。我想将它们都显示在flatlist中,但不知道如何访问第二组数据。
我正在从API请求中获取数据,然后将其设置为状态:
const [data1, setData1] = useState([]);
const [data2, setData2] = useState([]);
我将数据合并在一起:
const finalData = [...data1, ...data2]
我的flatlist:
<FlatList
data={finalData}
renderItem={_renderItem}>
</FlatList>
我的_renderitem:
const _renderItem = ({ item }) => (
<View>
<Image
source={{ uri: item.image }}
/>
<View style={{flexDirection: 'column', alignSelf: 'center', paddingLeft: 10 }}>
<Text>{item.title}</Text>
<Text>{item.username}</Text>
</View>
</View>
);
数据2的其余数据
我希望我的映像来自data1和我尝试查看的 进入截面列表,但也陷入了困境。
感谢任何帮助,谢谢!
I have two different datasets that i'm adding to different states. I want to display them both in a Flatlist but don't know how to access the second set of data.
I'm fetching data from an API request then setting it to state:
const [data1, setData1] = useState([]);
const [data2, setData2] = useState([]);
I merged data together:
const finalData = [...data1, ...data2]
My Flatlist:
<FlatList
data={finalData}
renderItem={_renderItem}>
</FlatList>
My _renderItem:
const _renderItem = ({ item }) => (
<View>
<Image
source={{ uri: item.image }}
/>
<View style={{flexDirection: 'column', alignSelf: 'center', paddingLeft: 10 }}>
<Text>{item.title}</Text>
<Text>{item.username}</Text>
</View>
</View>
);
I'd like for my Image to come from data1 and the rest of the data from data2
I've tried looking into Sectionlist but got stuck with that too.
Appreciate any help, thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想向一个项目显示,它来自第一个API,其余数据来自其他API,则可以轻松地执行以下方法:
If you want to show an item that something of it is from first api and rest of data are from other api, you are be able to easily do something like followings approach: