Answer the question
In order to leave comments, you need to log in
How to add unlimited elements in React?
It is necessary to add some html by clicking on the button. Let's say they clicked - a new input appeared. Another click - another appeared, etc.
How can this be implemented in pure React + ES6? Thank you.
Answer the question
In order to leave comments, you need to log in
Change the state of a component on button click.
#what a simple answer
Create some input component const input =
Create a list of inputs let list_inputs = [];
On click, we add an input to list_inputs: list_inputs.push(input).
Abstractly so.
Something like that
constructor(props) {
super(props);
this.state = {
data: []
};
};
renderInput () {
return this.state.data.map(item => {
return <div>
<input type="text" value={item}/>
</div>
})
}
addInput () {
let data = this.state.data;
data.push('Новый инпут');
this.setState({ data: data });
}
render() {
return (<div>
<button onClick={this.addInput.bind(this)}>Добавить</button>
{this.renderInput()}
</div>
)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question