It may seem like a repeated question, but the examples I found are complicated and need a pre-defined array, I do not have that, I'd like to know the simplest way to add an element to the DOM with React, it's like jQuery and I should change the state of the component.
Update 1 : I'm adding code to explain to Marcelo my problem better.
<tr>
<td>
<InputMask maxlenght="10" className="form-control" mask="99/99/9999" type="text"/>
</td>
<td>
<InputMask style={{'fontSize': '13px'}} mask="9999/99-99999" className="form-control" type="text"/>
</td>
<td>
<InputMask maxlenght="10" className="form-control" type="text"/>
</td>
<td >
<InputMask className="form-control" type="text"/>
</td>
<td>
<InputMask className="form-control somatorio" maskChar="0" mask="99:99" type="text"/>
</td>
</tr>
Well, I'd like to add this tr
to a tbody
, whenever the user clicks a button
, but when adding, it deletes the content already in tbody
.
Update 2 : I changed the way it is done, using state, follow the code
Component
constructor(props) {
super(props);
this.state = {
rows: []
};
}
insereRow() {
this.setState(prevState => ({
rows: [...prevState.rows, 'row1']
}))
}
Rendering in HTML, via map
{this.state.rows.map((rows) =>
<tr key={rows}>
<td>
{rows}
<InputMask maxlenght="10" className="form-control" mask="99/99/9999" type="text"/>
</td>
<td>
<InputMask style={{'fontSize': '13px'}} mask="9999/99-99999" className="form-control" type="text"/>
</td>
<td>
<InputMask maxlenght="10" className="form-control" type="text"/>
</td>
<td >
<InputMask className="form-control" type="text"/>
</td>
<td>
<InputMask className="form-control somatorio" maskchar="0" mask="99:99" type="text"/>
</td>
</tr>
)}
Button that should add an item in the array, so that another tr
<button type="button" onClick={this.insereRow} className="btn btn-primary"><i className="glyphicon glyphicon-plus"></i> Atividades
</button>
However this error in the this.setState = this is undefined line