R
R
Rustem2015-01-07 10:21:58
React
Rustem, 2015-01-07 10:21:58

Adding elements dynamically in React.js?

Good afternoon!
There is a task to draw a form of a memo, where in the "To:" field there can be from one to several recipients. Using react.js, I draw a form where I add a "To" button and one field  <input>.
I want to add more recipients when this button is pressed, but I don’t understand how to do this through react.js or is it necessary to do it through other libraries?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
123 123, 2015-01-28
@roman01la

let Form = React.createClass({
  getInitialState() {
    return {
      fieldsCount: 1
    };
  },
  _addField() {
    this.setState({ fieldsCount: this.state.fieldsCount + 1 });
  },
  render() {
    let fields = [];

    for (let i = 0; i < this.state.fieldsCount; i++) {
      fields.push(<input />);
    }

    return (
      <form>
        {fields}
        <button onClick={this._addField}>+</button>
      </form>
    );
  }
});

M
Maxim, 2016-03-12
@maxfarseer

Dynamic elements, input autofocus and other standard issues are covered in the react.js course for beginners in Russian.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question