react-native - React-Moment 给出 : "Invariant Violation: Text strings must be rendered within a <Text> component"

标签 react-native momentjs

我缩小了范围以发现错误仅在我尝试传递 React-Moment documentation 中描述的 <Moment /> 组件时发生。到目前为止,我还没有找到任何特定于这个包的解释,希望有人有类似的问题!

文档说明了这样的用法:

import Moment from 'react-moment';

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

我想采用这样的原始日期字符串:
<Text>Created {this.props.postDate}</Text>
存储方式如下:"postDate": "2019-01-31T04:13:31.224Z"
但到目前为止,无论何时我添加 <Moment>{this.props.postDate}</Moment> ,无论是在现有 <Text /> 块的外部还是内部,我都会变红:

enter image description here

最佳答案

它在文档中:

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

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

关于react-native - React-Moment 给出 : "Invariant Violation: Text strings must be rendered within a <Text> component",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497672/

相关文章:

ios - 如何实现 react-native-maps

React-Native:错误设置和从ClipBoard获取文本

javascript - 如何在多个 React Native 组件中保持当前变量的值

javascript - 如何使用时刻获取不同时区的日期之间的差异?

javascript - 将 unix 时间戳转换为 moment js 在 chrome 中无法正常工作

javascript - 在 Moment.js 中创建日期

android - React Native 构建不嵌入包文件

react-native - React Native 必须双击 onPress 才能工作

javascript - Moment.js - 如果日期处于不同日期,则无法正确计算日期之间的 .diff

javascript - 如何在 Typescript 中正确导入 MomentJS 解决以下错误 : "Type ' typeof moment' has no compatible call signatures.》