如何将输出值的顺序从JSON更改为FlatList?



我是React Native的新手,我不明白如何实现将输出值的序列从JSON格式更改为FlatList。假设我在JSON中的顺序是这样的:[id=1, name="test1"], [id=2, name="test2"],在FlatList中,它从1输出到2,我需要将输出从2反向到1。我该怎么做呢?

searchNews = async () =>
{
const ertAPI = await fetch(`${e_glav.e_url}/api/news/read.php?nocache`)
const APIValue = await ertAPI.json();
const APIResults = APIValue.ertnews
this.setState({
data:APIResults,
isFetching: false
})
}
<FlatList 
data={this.state.data}
onRefresh={() => this.onRefresh()}
refreshing={this.state.isFetching}
initialNumToRender={4} 
contentContainerStyle={{ alignSelf: 'stretch' }}
keyExtractor={({ id }, index) => id} 
renderItem={({item}) => (

如果你正在寻找一个自定义序列,你必须从后端(API)发送一个序列,基本上,从API发送排序数据或编写一个函数来生成你想要的序列数组。

如果您只是想要反转,那么执行以下操作

this.setState({
data:APIResults.reverse(), // <-- do this
isFetching: false
})

参考文件:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

最新更新