【问题标题】:show items based on the section根据部分显示项目
【发布时间】:2018-01-02 07:01:02
【问题描述】:

我想显示一个部分和与该部分相关的项目。我的意思是以下方式

过期

项目1

项目2

项目3

没有到期

项目1

项目2

项目3

我尝试了下面的方式,但是这种方式,我无法生成上面的ui。我该怎么做?

我有以下来自服务器的数据 开始时间, 时间结束, 提醒时间, 标题, _id

这是我尝试过的

const
    ListItem = ({task}) => {
        const
            current_time = new Date();
        if (current_time > task.end_time) {
            return <View key={task._id} style={{flexDirection: 'column', flex: 1}}>
                <Text>overdue</Text>
                <Text>{task.title}</Text>
                <Text>{task.start_time}</Text>
            </View>
        }
        return (
            <View key={task._id} style={{flexDirection: 'column', flex: 1}}>
                <Text>{task.title}</Text>
                <Text>{task.start_time}</Text>
            </View>
        )
    }

const List = ({query}) => {
    console.log('query', query);
    if(query.loading) return <Text>Loading...</Text>
    if(query.error) return <Text>Error...</Text>
    return (
        <Wrapper>
                <View style={{flexDirection: 'row', marginLeft: 10, marginTop: 10}}>
                    <Image
                        resizeMode="contain"
                        source={newIcon}
                        style={{width: 25, height: 25}}
                    />
                    <Text
                        style={{
                            alignSelf: 'center',
                            color: '#1F84DD'
                        }}>
                        New Task
                    </Text>
                </View>
                <FlatList
                    data={query.tasks}
                    renderItem={({item}) => <ListItem task={item} />}
                />
        </Wrapper>
    )
}

尝试使用SectionList如下方式

<SectionList
    sections={[
        {title: 'Overdue', data:query.tasks.filter(task => task.end_time < new Date())},
        {title: 'Upcoming', data:query.tasks.filter(task => task.end_time > new Date())}
    ]}
    renderItem={({item}) => <Text>{item}</Text>}
    renderSectionHeader={
        ({section}) => <Text>{section.title}</Text>
    }
/>

这样我得到错误

不变违规:对象作为 React 子项无效(找到:对象 带键 _id、start_time、end_time、remincer_time)

【问题讨论】:

  • 当前代码遇到了什么问题?
  • 我得到这样的数据,如逾期项目1、没有到期项目1、没有到期项目2、逾期项目1、逾期项目2。
  • 我无法以结构化的方式展示它们。我的意思是所有在到期日部分到期的项目和所有没有到期日的项目都应该显示在没有到期日部分而不是曲折的随机方式
  • 我看到有 SectionList 但我不知道应该如何创建 sectionData 因为每个部分都应该有它的数据。我是否根据每个部分的时间计算过滤项目?
  • 这是因为您的 ListItem 有条件地返回项目对吗?如,根据当前时间条件,它同时返回过期或未到期。对于结构化格式,您需要先获取所有过期的项目,然后获取未到期的列表项目。

标签: javascript reactjs react-native


【解决方案1】:

我看到了一种可能的解决方案。在您的部分列表代码中,您正在文本组件内打印项目对象,它应该是标题。 将 renderItem={({item}) => {item}} 替换为 renderItem={({item}) => {item.titke}}。

谢谢。

【讨论】:

  • 我以这种格式获得时间(2016-08-05T08:38:50.142Z)。用这些格式计算到期日和没有到期日的方法是什么?
  • 您好,您可以使用 moment js (momentjs.com/docs) 来实现此功能。将给定日期转换为时间戳。让 x = moment("2016-08-05T08:38:50.142Z", "YYYY-MM-DDTHH:mm:ss.SSSZ").unix();并将当前时间转换为时间戳。让 y = moment().unix();那么如果 x - y > 0 日期在未来。否则,如果 x - y
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 2018-12-19
  • 2021-01-02
相关资源
最近更新 更多