Skip to content

Instantly share code, notes, and snippets.

@ShlomiRex
Created January 29, 2022 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShlomiRex/39bdb489bff745197ef57b749f871bb9 to your computer and use it in GitHub Desktop.
Save ShlomiRex/39bdb489bff745197ef57b749f871bb9 to your computer and use it in GitHub Desktop.
Dynamically add children to parent component using a button in reactjs
import * as React from 'react';
import ReactDOM from "react-dom";
class Child extends React.Component {
render() {
return <div>Hi!</div>
}
}
class Parent extends React.Component {
state: any
constructor(props) {
super(props);
this.state = {children: []}
// Give handleClick access to 'this'.
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// Option 1
// this.setState({
// children: [...this.state.children, { text: "hi" }]
// });
// Option 2
this.setState({
children: this.state.children.concat([{ text: "hi" }])
});
}
render() {
return <div>
<h1>Hello from parent!</h1>
<button onClick={this.handleClick}>
Add children
</button>
<div>
Children ({this.state.children.length}):
{
this.state.children.map((child) => {
return <Child></Child>
})
}
</div>
</div>
}
}
export default Parent;
const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment