I'm new to React and I'm trying to add objects one by one to an array in state when I submit a form but I only get one object in the array, here's a code snippet for that:
MY STATE
state = {
comments: [],
text: ""
};
HANDLE FORM SUBMIT
handleSubmit = event => {
event.preventDefault();
const arr = [];
arr.push({ name: this.state.text, id: Math.random() });
this.setState({ comments: arr, text: "" });
};
RENDER METHOD
<form onSubmit={this.handleSubmit}>
<input
type="text"
value={this.state.text}
onChange={e => this.setState({ text: e.target.value })}
/>
<ul>{this.renderComments()}</ul>
</form>