Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ichengzi/75b4e05a264fc2e6fa6aaef45332f101 to your computer and use it in GitHub Desktop.
Save ichengzi/75b4e05a264fc2e6fa6aaef45332f101 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
class AppComponent extends React.Component {
state = {
numChildren: 0
}
render () {
const children = [];
for (var i = 0; i < this.state.numChildren; i += 1) {
children.push(<ChildComponent key={i} number={i} />);
};
return (
<ParentComponent addChild={this.onAddChild}>
{children}
</ParentComponent>
);
}
onAddChild = () => {
this.setState({
numChildren: this.state.numChildren + 1
});
}
}
const ParentComponent = props => (
<div className="card calculator">
<p><a href="#" onClick={props.addChild}>Add Another Child Component</a></p>
<div id="children-pane">
{props.children}
</div>
</div>
);
const ChildComponent = props => <div>{"I am child " + props.number}</div>;
ReactDOM.render(<AppComponent/>, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment