簡體   English   中英

對象無效作為React子對象

[英]Objects are not valid as React child

我有一個功能,可以從Unix時間戳計算日期

_getTime(time) {
var dateTime = new Date(time*1000);

console.log(dateTime);
return dateTime

  },

該功能用於此

render: function() {
    return (    
       {this.state.daily.map(day => {
          return (
             <div key={day.time} className="key col-md-12">
               <div className="col-md-3">{this._getTime(day.time)}</div>
             </div>
          );
    );
},

這將返回Invariant Violation: Objects are not valid as a React child (found: Thu Oct 20 2016 00:00:00 GMT+0200 (CEST)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Invariant Violation: Objects are not valid as a React child (found: Thu Oct 20 2016 00:00:00 GMT+0200 (CEST)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Invariant Violation: Objects are not valid as a React child (found: Thu Oct 20 2016 00:00:00 GMT+0200 (CEST)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of App Invariant Violation: Objects are not valid as a React child (found: Thu Oct 20 2016 00:00:00 GMT+0200 (CEST)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of .

我知道有相同錯誤消息的問題,但似乎無法為我的問題找到解決方案。

普通的javascript:

_getTime(time) {
    var dateTime = new Date(time*1000).toString();    
    console.log(dateTime);
    return dateTime    
 },

使用moment.js

_getTime(time) {
    var dateTime = moment(time*1000).format('YYYY-MM-DD HH:mm:ss');    
    console.log(dateTime);
    return dateTime    
 },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM