如何设置状态?

时间:2019-05-31 19:40:34

标签: javascript reactjs setstate

在onchange中设置setState()并尝试更改该值时,它仅写入或删除1个字符。有什么问题吗?

enter code here:

  constructor() {
      super();
       this.state = {lw1val:null}
   }

   {this.state.isLw1 ? (
                <input 
                onChange={e => {
                  this.setState({ lw1val: e.target.value })

                }}
                value={this.state.lw1val}
                  onKeyPress={event => {

                    if (event.keyCode === 13 || event.which === 13) {
                      var inPutData = [
                        {
                          filterType: "is-equal-to",
                          value: event.target.value
                        }
                      ];

                      onChange(inPutData);
                    }
                  }}
                />
              ) : null}

1 个答案:

答案 0 :(得分:3)

在输入代码中,指定哪个是您的值:

<input value={this.state.lw1val} onChange= {event => this.setState({ lw1val: event.target.value })} />