对象无效作为React子对象

时间:2016-10-20 18:04:03

标签: reactjs

我有一个从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应用.

我知道有相同错误消息的问题,但我似乎无法找到解决问题的方法。

1 个答案:

答案 0 :(得分:3)

普通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    
 },