【问题标题】:React-Moment gives: "Invariant Violation: Text strings must be rendered within a <Text> component"React-Moment 给出:“不变违规:文本字符串必须在 <Text> 组件中呈现”
【发布时间】:2019-06-27 02:28:55
【问题描述】:

我已经缩小范围,发现只有当我尝试传递&lt;Moment /&gt; 组件时才会发生错误,如React-Moment documentation 中所述。到目前为止,我还没有找到任何特定于这个包的解释,希望有人遇到过类似的问题!

文档说明了这样的用法:

import Moment from 'react-moment';

// then within the class component:
return (
        const dateToFormat = '1976-04-19T12:59-0500';
        <Moment>{dateToFormat}</Moment>
        );

我想采用这样的原始日期字符串:

&lt;Text&gt;Created {this.props.postDate}&lt;/Text&gt;

这样存储的:"postDate": "2019-01-31T04:13:31.224Z"

但到目前为止,无论何时我添加 &lt;Moment&gt;{this.props.postDate}&lt;/Moment&gt;,无论是在现有 &lt;Text /&gt; 块的外部还是内部,我都会变红:

【问题讨论】:

    标签: react-native momentjs


    【解决方案1】:

    在文档中:

    https://github.com/headzoo/react-moment#usage-with-react-native

    <Moment element={Text}>{dateToFormat}</Moment>
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2019-02-21
      • 2020-01-20
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多