admin管理员组文章数量:1026989
I'd like to insert dynamically several React ponent in a div HTML
element. Every time that I click on button, I would like to add a new PeriodButtonContainer
inside div with id="container"
.
This is what I've tried so far:
class Period extends React.Component {
addPeriodHandler = () => {
/****?????******/
};
render() {
return (
<div>
<div id="container">
<PeriodButtonContainer />
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
Thanks in advance
I'd like to insert dynamically several React ponent in a div HTML
element. Every time that I click on button, I would like to add a new PeriodButtonContainer
inside div with id="container"
.
This is what I've tried so far:
class Period extends React.Component {
addPeriodHandler = () => {
/****?????******/
};
render() {
return (
<div>
<div id="container">
<PeriodButtonContainer />
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
Thanks in advance
Share Improve this question edited Jul 11, 2018 at 21:23 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Jul 11, 2018 at 21:19 M.MorrisM.Morris 1473 silver badges13 bronze badges 1- you should increment a counter in your state, create an array of the length of the counter and map it to your element – Axnyff Commented Jul 11, 2018 at 21:22
1 Answer
Reset to default 6You could keep a counter that you increment on each button click, and create that many PeriodButtonContainer
ponents in your render method with e.g. Array.from
.
Example
class Period extends React.Component {
state = { periods: 1 };
addPeriodHandler = () => {
this.setState(previousState => {
return { periods: previousState.periods + 1 };
});
};
render() {
return (
<div>
<div id="container">
{Array.from({ length: this.state.periods }, (_, index) => (
<PeriodButtonContainer key={index} />
))}
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
I'd like to insert dynamically several React ponent in a div HTML
element. Every time that I click on button, I would like to add a new PeriodButtonContainer
inside div with id="container"
.
This is what I've tried so far:
class Period extends React.Component {
addPeriodHandler = () => {
/****?????******/
};
render() {
return (
<div>
<div id="container">
<PeriodButtonContainer />
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
Thanks in advance
I'd like to insert dynamically several React ponent in a div HTML
element. Every time that I click on button, I would like to add a new PeriodButtonContainer
inside div with id="container"
.
This is what I've tried so far:
class Period extends React.Component {
addPeriodHandler = () => {
/****?????******/
};
render() {
return (
<div>
<div id="container">
<PeriodButtonContainer />
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
Thanks in advance
Share Improve this question edited Jul 11, 2018 at 21:23 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Jul 11, 2018 at 21:19 M.MorrisM.Morris 1473 silver badges13 bronze badges 1- you should increment a counter in your state, create an array of the length of the counter and map it to your element – Axnyff Commented Jul 11, 2018 at 21:22
1 Answer
Reset to default 6You could keep a counter that you increment on each button click, and create that many PeriodButtonContainer
ponents in your render method with e.g. Array.from
.
Example
class Period extends React.Component {
state = { periods: 1 };
addPeriodHandler = () => {
this.setState(previousState => {
return { periods: previousState.periods + 1 };
});
};
render() {
return (
<div>
<div id="container">
{Array.from({ length: this.state.periods }, (_, index) => (
<PeriodButtonContainer key={index} />
))}
</div>
<div>
<button id="addPeriod" onClick={this.addPeriodHandler}>
Add a period
</button>
</div>
</div>
);
}
}
本文标签: javascriptInsert dynamically a React component in a divStack Overflow
版权声明:本文标题:javascript - Insert dynamically a React component in a div - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660416a2161855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论