React.render replace container instead of inserting into

前端 未结 4 709
花落未央
花落未央 2020-12-09 16:05

I\'m gradually replacing some Backbone views with React.

My React View:

Rendered By React
4条回答
  •  生来不讨喜
    2020-12-09 16:50

    You don't need jQuery to work around this issue.

    You just need to render into a temporary DIV and extract the content and replace the existing element. I've added the id="destination" so that the element can be easily retrieved from the temporary element.

    var Hello = React.createClass({
        render: function() {
            return 
    Hello {this.props.name}
    ; } }); // temporary render target var temp = document.createElement("div"); // render React.render(, temp); // grab the container var container = document.getElementById("container"); // and replace the child container.replaceChild(temp.querySelector("#destination"), document.getElementById("destination"));

提交回复
热议问题