Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React + Backbone, Target container is not a DOM element

Note: This is a react error being thrown.

So I am trying an experiment where I render a post component from a backbone router based on that page. Now I know you would not typically do things this way, things can get messy and such. But again It's just an experiment.

So I have the following route in backbone (Note the react call):

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.');
  }

});

The idea is that it should spit out a post title on to the page, one right after the other (I have a feeling it will only do it once).

But the issue is that the console is saying:

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

Is this because of how I am trying to render the component?

like image 752
user3379926 Avatar asked Dec 11 '22 05:12

user3379926


1 Answers

You're setting this.mountNode to $('#blog-manage'), which is a jQuery object (essentially an array of nodes). If you instead do

mountNode: $('#blog-manage')[0]

or

mountNode: $('#blog-manage').get(0)

then you'll have reference to the actual DOM node.

like image 104
Sophie Alpert Avatar answered Jan 01 '23 23:01

Sophie Alpert