React + Backbone,目标容器不是DOM元素

use*_*926 4 javascript backbone.js reactjs

注意:这是抛出的反应错误.

所以我正在尝试一个实验,我根据该页面从骨干路由器渲染一个post组件.现在我知道你通常不会这样做,事情会变得混乱等等.但是,这只是一个实验.

所以我在骨干网中有以下路线(注意反应呼叫):

AisisWriter.Routers.Posts = Backbone.Router.extend({

  writer_posts: null,

  posts: null,

  mountNode: $('#blog-manage'),

  routes : {
      '': 'index'
  },

  initialize: function() {
    this.writer_posts = new AisisWriter.Collections.Posts();
  },

  index: function() {
    var options = { reset: true };
    this.writer_posts.fetch(options).then(this.postsRecieved, this.serverError);
  },

  // Deal with the posts received
  postsRecieved: function(collection, response, options) {
    this.posts = collection;

    // Walk through the posts.
    $.each(this.posts, function(key, value){
      // value is an array of objects.
      $.each(value, function(index, post_object){
        React.renderComponent(new Post({post: post_object}), this.mountNode);
      });
    });
  },

  // Deal with any server errors
  serverError: function() {
    console.log('There was an error trying to fetch the posts.');
  }

});
Run Code Online (Sandbox Code Playgroud)

这个想法是它应该在页面上吐出一个帖子标题,一个接着一个(我觉得它只会做一次).

但问题是控制台说:

Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element.

这是因为我试图渲染组件的方式吗?

Sop*_*ert 10

你设置this.mountNode$('#blog-manage'),这是一个jQuery对象(基本上节点阵列).如果你做了

mountNode: $('#blog-manage')[0]
Run Code Online (Sandbox Code Playgroud)

要么

mountNode: $('#blog-manage').get(0)
Run Code Online (Sandbox Code Playgroud)

那么你将引用实际的DOM节点.