【问题标题】:why sectionlist render string in one line react native为什么sectionlist在一行中呈现字符串反应原生
【发布时间】: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


    【解决方案1】:

    您一次渲染部分中的所有数据,一次只需要渲染一项。

    在你的平面列表中:

    renderItem={({item, index, section}) => <Text key={index}>{item}</Text>}
    

    【讨论】:

    • 如果我需要渲染更多的东西然后命名,我应该怎么做
    • 如果您的数据结构类似于 [{name: "title", age: 34}],只需像标准对象 {item.age} 一样使用该属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多