admin管理员组文章数量:1287484
Say <ponentX \>
when onClick of "create box" button, the ponentX should append inside of box-container. If i click create box 3 times, then three ponentX should append inside box-container (It's not that simply keeping the ponent then hide and show when click of create box). What are all the ways to achieve this in ReactJS.
import ComponentX from './ComponentX.jsx';
class App extends React.Component{
constructor(){
super();
this.state = {
}
}
render(){
let board = Box;
return(
<div>
<a onClick={}>Create Box</a>
<div className="box-container"></div>
</div>
);
}
}
export default App;
Say <ponentX \>
when onClick of "create box" button, the ponentX should append inside of box-container. If i click create box 3 times, then three ponentX should append inside box-container (It's not that simply keeping the ponent then hide and show when click of create box). What are all the ways to achieve this in ReactJS.
import ComponentX from './ComponentX.jsx';
class App extends React.Component{
constructor(){
super();
this.state = {
}
}
render(){
let board = Box;
return(
<div>
<a onClick={}>Create Box</a>
<div className="box-container"></div>
</div>
);
}
}
export default App;
Share
Improve this question
edited Sep 8, 2018 at 17:40
jamal
asked Sep 8, 2018 at 17:08
jamaljamal
531 gold badge1 silver badge5 bronze badges
2 Answers
Reset to default 8Try something like this:
import ComponentX from './ComponentX.jsx';
class App extends React.Component{
constructor(){
super();
this.state = {
children: [];
}
}
appendChild(){
this.setState({
children: [
...children,
<ponentX \>
]
});
}
render(){
let board = Box;
return(
<div>
<a onClick={() => this.appendChild()}>Create Box</a>
<div className="box-container">
{this.state.children.map(child => child)}
</div>
</div>
);
}
}
export default App;
You can conditionally render by using ponent state like this:
import ComponentX from './ComponentX.jsx';
class App extends React.Component{
constructor(){
super();
this.state = {
showComp = false;
}
}
handleClick = () => {
this.setState({
showComp: true,
})
}
render(){
let board = Box;
const { showComp } = this.state;
return(
<div>
<a onClick={this.handleClick}>Create Box</a>
<div className="box-container">
{showComp && <ComponentX />
</div>
</div>
);
}
}
export default App;
本文标签: javascriptReactJs How to addappend a component on click of buttonStack Overflow
版权声明:本文标题:javascript - ReactJs: How to addappend a component on click of button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741308303a2371502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论