【问题标题】:React Native Moment Error: Expected a component class, got [object Object]React Native Moment 错误:需要一个组件类,得到 [object Object]
【发布时间】:2018-03-21 02:20:59
【问题描述】:

我阅读了至少八篇关于标题错误的 Stack Overflow 帖子,但我还没有解决这个问题,因为我将 <>s 中的所有内容都大写并且不使用 HTML 标记。使用 Moment 文档作为指导,我试图在第一个 <Moment></Moment> 中显示一周中的当前日期,并在第二个中显示 2017 年 10 月 9 日这样的日期。下面我粘贴了我的 App.js。我使用 create-react-native 应用程序开始。我应该怎么做?

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Moment from 'react-moment';


export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Moment format="dddd"></Moment>
        <Moment format='MMMM Do YYYY'></Moment>
        <Text>Today at a Glance</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
  },
});

【问题讨论】:

  • 删除以下行时组件是否呈现?:&lt;Moment format="dddd"&gt;&lt;/Moment&gt; &lt;Moment format='MMMM Do YYYY'&gt;&lt;/Moment&gt;
  • 你给出了一个格式,但没有给出任何格式的日期。
  • 是的 223seneca,当我注释掉组件呈现的那些行时。此外,斯多葛哲学摇滚!
  • 我还尝试在 Moment XML 之间传递 {new Date()},bennygenel。顺便说一句好听的名字
  • 另外,我尝试了下面的代码,仍然得到这个页面标题中的错误:

标签: react-native momentjs


【解决方案1】:

我有同样的问题,我在文档中找到了这个: &lt;Moment element={Text} &gt;1976-04-19T12:59-0500&lt;/Moment&gt;

我们需要传递元素。错误消息没有帮助,但这样做对我有用

文档链接: https://github.com/headzoo/react-moment#usage-with-react-native

【讨论】:

  • 谢谢 rmovieira!当我将渲染功能更改为以下时,应用程序正确渲染:
  • render() { let now = new Date(); return ( {now}{now 今天一瞥 ); }
猜你喜欢
  • 2017-05-11
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 2021-03-31
  • 2020-09-25
  • 1970-01-01
相关资源
最近更新 更多