Как напечатать новый Дата JavaScript () в React? - PullRequest
0 голосов
/ 25 апреля 2018

Я не могу печатать простые даты JavaScript внутри React.

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()

Ответы [ 2 ]

0 голосов
/ 25 апреля 2018

new Date(); дает вам объект. Вы не можете визуализировать объекты как детей в React. Вы можете получить строковую версию даты с toString().

0 голосов
/ 25 апреля 2018

Date объект должен быть приведен к строке. Вы можете попробовать с

{str.toString()}

или

{`${str}`}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...