如何调试此错误:未捕获(在promise中)错误:对象无效作为React子

时间:2016-01-21 08:51:56

标签: javascript reactjs

控制台中的完整错误:

  

未捕获(在承诺中)错误:对象无效作为React子对象(找到:具有键{id,name,description,css,ephemeral,readonly,topPost}的对象)   如果您要渲染子集合,请使用数组,或使用React附加组件中的createFragment(object)包装对象。检查exports。(...)

的渲染方法

我真的不知道这个错误意味着什么,它并没有指向代码中的一行,所以我不知道该怎么做。

我正在使用 api.jsx 从Imgur获取数据(特别是我在 topic-store.jsx 中调用它),然后尝试在主题-list.jsx

main.jsx

var React = require('react');
var Header = require('./header');
var TopicList = require('./topic-list');

module.exports = React.createClass({
    render: function () {
        return <div>
          <Header />
          {this.content()}
        </div>
    },
    content: function () {
        if (this.props.children) {
            return this.props.children
        } else {
            return <TopicList/>
        }
    }
});

header.jsx

var React = require('react');
var Router = require('react-router');
var Link = Router.Link; //Router's Link object is a renderable component, that turns into an anchor tag when rendered
//Using Link allows a user to change routes without triggering a full page refresh, the content on the page will change but the browser will not refresh

module.exports = React.createClass({
    render: function () {
        return <nav className="navbar navbar-default header">
          <div className="container-fluid">
            <Link to="/" className="navbar-brand">
              Imgur Browser
            </Link>
            <ul className="nav navbar-nav navbar-right">
              <li><a>Topic #1</a></li>
            </ul>
          </div>
        </nav>
    }
});

主题的list.jsx

var React = require('react');
var TopicStore = require('../stores/topic-store');

module.exports = React.createClass({

    getInitialState: function () {
        return {topics: []}
    },

    componentWillMount: function () {
        TopicStore.getTopics().then(function () {
            //We have successfully fetched topics
            //Topics are available on TopicStore.topics
            this.setState({
                topics: TopicStore.topics
            });
        }.bind(this));
    },

    render: function () {
        return <div className="list-group">
          Topic List
          {this.renderTopics()}
        </div>
    },

    renderTopics: function () {
        return this.state.topics.map(function(topic) {
            return <li>
              {topic}
            </li>
        });
    }
});

主题的store.jsx

var Api = require('../utils/api');
var Reflux = require('reflux');

module.exports = Reflux.createStore({

    getTopics: function() {

        return Api.get('topics/defaults').then(function(json) {

            this.topics = json.data;

        }.bind(this));
    }
});

api.jsx

var Fetch = require('whatwg-fetch');
var rootUrl = 'https://api.imgur.com/3/';
var apiKey = 'e80dc51eb3f6d56';

module.exports = window.api = {
    get: function(url) {
        return fetch(rootUrl + url, {
            headers: {
                'Authorization': 'Client-ID ' + apiKey
            }
        }).then(function (response) {
            return response.json()
        });
    }
};

3 个答案:

答案 0 :(得分:24)

问题取决于您在renderTopics方法中呈现主题对象的方式。

当你做这样的事情时:

return <li>{topic}</li>

你基本上是想做:

return <li>{{ id: 1, name: 'One topic' }}</li>

并且React不知道如何渲染原始对象。要解决您的问题,请指定要渲染的对象的哪些键。例如:

renderTopics: function () {
  return this.state.topics.map(function(topic) {
    return (<li>{topic.id} {topic.name}</li>)
  });
}

答案 1 :(得分:2)

topic-list.jsx

中缺少<ul></ul><ol></ol>标记

在主题的渲染调用中使用<ul></ul>标记:

render: function () {
    return <div className="list-group">
      Topic List
      <ul>
      {this.renderTopics()}
      </ul>
    </div>
},

更新:合并Aperçu中的评论以确保完整性

您需要从json blob获取值(不呈现Raw内容):

主题为{id:1, name:Topic1}

renderTopics: function () {
    return this.state.topics.map(function(topic) {
        return <li>
          {topic.id}{topic.name}
        </li>
    });
}

答案 2 :(得分:0)

要完成以前的答案,请在渲染函数中返回的JSX中添加括号

例如 main.jsx

var React = require('react');
var Header = require('./header');
var TopicList = require('./topic-list');

module.exports = React.createClass({
    render: function () {
        return (
          <div>
            <Header />
            {this.content()}
          </div>
        );
    },
    content: function () {
        if (this.props.children) {
            return this.props.children
        } else {
            return <TopicList/>
        }
    }
});