Add Inputs in React with add button

前端 未结 3 927
花落未央
花落未央 2021-01-25 23:20

I\'m trying to make a set of inputs that can be duplicated or removed.

I\'ve found and used a combination of this : https://jsfiddle.net/69z2wepo/36745/ and this (becaus

3条回答
  •  死守一世寂寞
    2021-01-25 23:39

    Remove const count and initialize a count variable in state.

    constructor(props) {
            super(props);
            this.state = { inputList: [], count: 0 };
            this.incrementCount = this.incrementCount.bind(this);
            this.decrementCount = this.decrementCount.bind(this);
        }
    

    Then use this.state.count as key in input element:

    incrementCount() {
          const inputList = this.state.inputList;
            this.setState({
                count: this.state.count + 1,
                inputList: inputList.concat(),
            });
        }
        decrementCount() {
          const inputList = this.state.inputList;
            this.setState({
                count: this.state.count - 1,
                inputList: inputList.concat(),
            });
        }
    

提交回复
热议问题