Question

Consider the following Component hierarchy:

var FundComponent = React.createClass({
    render : function() {
      return (
        <div>
        <UserComponent profile={data} />
        </div>
        );
    }
  });

var UserComponent = React.createClass({
    render : function() {
      return (
        <div>
        Some Stuff goes here
        </div>
        );
    }
  });

React.renderComponent(<FundComponent />, document.getElementById('fund'));

This will render UserComponent inside the #fund node in my HTML.

How can I render UserComponent in a specific node inside the fund node in my HTML ? eg:

<div id="fund">
<div id="otherStuff">Stuff</div>
<div id="user">**load UserComponentHere**</div>
</div>
Était-ce utile?

La solution

After reading around a bit, I found out how to go about this. Feel free to correct me.

React has a different concept as to how to go about this.

Rather than pre-defining the html in the html file, we define it in the render function, like so:

var FundComponent = React.createClass({
    render : function() {
      return (
        <div>
          <div className="myOtherStuff1">
            <OtherComponent1 data={data} />  //other stuff
          </div>
          <div className="user">  //user
            <UserComponent profile={profile} />
          </div>
          <div className="myOtherStuff2">
            //can have component or other static stuff. Upto developer.
          </div>
          ...
        </div>
      );
    }
  });

React.renderComponent(<FundComponent />, document.getElementById('fund'));

And the html will be: <div id="fund"></div>

And then, we can define UserComponent and OtherComponent(s) as we go.

Autres conseils

I may be misunderstanding you, but you should simply be able to do:

React.renderComponent(<FundComponent />, document.getElementById('user'));

to render into the #user div.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top