My goal is to add components dynamically on a page/parent component.
I started with some basic example template like this:
main.js:
var App = require('./App.js');
var SampleComponent = require('./SampleComponent.js');
ReactDOM.render(<App/>, document.body);
ReactDOM.render(<SampleComponent name="SomeName"/>, document.getElementById('myId'));
App.js:
var App = React.createClass({
render: function() {
return (
<div>
<h1>App main component! </h1>
<div id="myId">myId div</div>
</div>
);
}
});
SampleComponent.js:
var SampleComponent = React.createClass({
render: function() {
return (
<div>
<h1>Sample Component! </h1>
</div>
);
}
});
Here SampleComponent
is mounted to <div id="myId"></div>
node, which is pre-written in App.js
template. But what if I need to add indefinite number of components to App component? Obviously I cannot have all the required divs sitting there.
After reading some tutorials I still have no understanding of how components are created and added to parent component dynamically. What is a way of doing it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…