单击按钮后,我想在div标签(id = item)下呈现。如果我两次单击按钮,应呈现两个div标签(id = item)。如何在React中做到这一点js ??
<div id="item">
<div class="form-group">
<label class="col-lg-3 control-label">Item name:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="Rice n Curry"/>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Price (Rs):</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="150.00"/>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Available Quantity:</label>
<div class="col-lg-8">
<input class="form-control" type="text" value="10"/>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Item image:</label>
<div class="col-lg-8">
<input type="file" class="form-control"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<button type="button" class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-trash"></span> Delete this item
</button>
</div>
</div>
答案 0 :(得分:1)
class App extends React.Component {
state = {
arrayOfDivs: []
};
createDiv = () => {
let { arrayOfDivs } = this.state;
arrayOfDivs = arrayOfDivs.concat([<div> Hello </div>]);
this.setState({ arrayOfDivs });
};
render() {
const { arrayOfDivs } = this.state;
return (
<div>
<h1> Generate Div On Click </h1>
<button onClick={this.createDiv}> Generate </button>
<div>
{arrayOfDivs.length ? arrayOfDivs.map(item => item) : "empty array"}
</div>
</div>
);
}
}
创建一个将div推入数组并在render方法中映射该数组并显示每个div的函数。