渲染React组件并使用setstate

时间:2018-07-08 20:47:44

标签: javascript reactjs

我有一个React组件,可以通过api获取一些数据。该数据数组中的每个项目都包含一个日期和一些文本。因为我想像这样显示它:

DAY ---文字 ---文字 ---文字 天 ---文字 ---文字 ---文字

我需要跟踪获取的最后日期。我现在要尝试的是从我的 componentDidMount

调用此函数

setDates() {
    const logs = this.props.activities.map((post) => {
      const currentDate = post.created_at;
      if (this.state.lastDate !== currentDate) {
        return this.setState({
          lastDate: currentDate,
        }, () => (
          <div className="recent__items">
            <div className="recent__date">
              {currentDate} asasas
              </div>
            <Activity key={post.id} data={post} />
          </div>
          ));
      }
      return (
        <div className="recent__items">
          teste
          <Activity key={post.id} data={post} />
        </div>
      );
    });

    this.setState({
      activities: logs,
    });
  }

由于某些原因,日志始终是空值数组。

有人可以告诉我怎么了,我该怎么做呢?

0 个答案:

没有答案