【发布时间】:2021-07-07 11:26:07
【问题描述】:
我通过使用reduce 函数在本机反应中按查询创建组,然后我创建部分列表来显示数据,但我有一些问题。 我的代码
<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
我只想针对每个标题每行显示一个名称,但根据我的代码渲染工作正常,因为第一个标题有两条记录,所以它渲染两次,但两个名称在同一行两次
【问题讨论】:
标签: react-native react-native-sectionlist