【问题标题】:React - rerender relative time every few secondsReact - 每隔几秒重新渲染一次相对时间
【发布时间】:2016-09-23 08:03:18
【问题描述】:

我正在使用 React 创建一个实时应用程序。 我使用 Mobx 作为我的状态容器。 (没关系,雷迪

我有一个显示相对时间的组件,因为最后一个订单已经到达。 (<FormattedRelative /> 组件取自 react-intl 库 - 显示类似“1 分钟前”的内容)。 第一次呈现 UI 时,此“1 分钟前”消息会被快照。但随着时间的推移,这会变得过时且不准确。

您认为使这些消息保持最新(假设最多 10 秒)的最佳和惯用方法是什么?

我应该以某种方式安排使用 setInterval 重新渲染吗?或者我应该定期更改商店中的某些值,以便重新渲染我的 UI?

感谢您的任何建议。

【问题讨论】:

    标签: javascript reactjs time


    【解决方案1】:

    尝试使用react-timeago。它是高度可定制的国际使用,支持本地化 例如:- 带有法语字符串的自定义格式化程序

    import TimeAgo from 'react-timeago'
    import frenchStrings from 'react-timeago/lib/language-strings/fr'
    import buildFormatter from 'react-timeago/lib/formatters/buildFormatter'
    
    const formatter = buildFormatter(frenchStrings)
    
    // in your react component
    <TimeAgo date='Feb 1, 1966' formatter={formatter} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多