我使用reduce函数在react原生中创建group by查询,然后创建区段列表来显示数据,但我有一些问题。我的代码
<View>
<SectionList
renderSectionHeader={({ section: { title} }) => (
<Text style={{ fontWeight: 'bold' }}>{title}</Text>
)}
sections={this.state.dataSource}
renderItem={({item, index, section}) => <Text key={index}>{section.data}</Text>}
keyExtractor={(item, index) => item + index}
/>
</View>
const dataSource = responseJson.old_cases.reduce(function (sections, item) {
let section = sections.find(section => section.gender === item.gender);
if (!section) {
section = { gender: item.gender,data:[] };
sections.push(section);
}
section.data.push(item.name)
return sections;
}, []);
this.setState({dataSource: dataSource // Pass the dataSource that we've processed above});
但是我的输出是这样的
title //header
name_1,name2
name_1,name2
title2 //header
name_3
我想要的输出
title //header
name_1
name2
title2 //header
name_3
我只想在每个标题的每行显示一个名字,但是根据我的代码,渲染工作很好,因为第一个标题有两个记录,所以它渲染了两次,但两个名字在同一行上两次
发布于 2021-07-07 11:44:56
要一次呈现区段中的所有数据,您只需要一次呈现一个项目。
在您的flatList中:
renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
https://stackoverflow.com/questions/68285118
复制