【发布时间】:2018-04-25 14:12:23
【问题描述】:
我无法在 React 中打印简单的 JavaScript 日期。
import React, { Component } from 'react';
import './ActivityWarningText.css';
class ActivityWarningText extends Component {
render() {
const str = new Date();
return(
<div>
<h1>All activity is recorded. {str}</h1>
</div>
)
}
}
export default ActivityWarningText;
我收到此错误:
Objects are not valid as a React child (found: Wed Apr 25 2018 18:09:03 GMT+0400 (Arabian Standard Time)). If you meant to render a collection of children, use an array instead.
in h1 (at index.js:17)
in div (at index.js:16)
in ActivityWarningText (at index.js:34)
in component (created by Route)
in Route (at index.js:34)
in div (at index.js:33)
in div (at index.js:30)
in Router (created by BrowserRouter)
in BrowserRouter (at index.js:29)
in App (at withAuthentication.js:36)
in WithAuthentication (at index.js:14)
不过,这可行,str.getFullYear()
【问题讨论】:
-
这个错误告诉你你需要知道的一切。如果
str是对象,则{str}无效。它必须是一个字符串。Date提供了很多转换为字符串的函数,如date.toISOString()或date.toDateString();
标签: javascript reactjs datetime