小编典典

使用onBlur事件的值更新React Input文本字段

reactjs

我有以下输入字段,如下所示。模糊时,该函数调用服务以将输入值更新到服务器,完成后,它将更新输入字段。

我该如何运作?我能理解为什么它不能让我更改字段,但是我可以做些什么才能使其起作用?

我无法使用,defaultValue因为我会将这些字段更改为其他字段

<input value={this.props.inputValue} onBlur={this.props.actions.updateInput} />


阅读 1118

收藏
2020-07-22

共1个答案

小编典典

为了使输入值可编辑,您需要具有onChange用于更新输入值的处理程序。并且由于要调用onBlur函数,因此必须像onBlur={() => this.props.actions.updateInput()}

componentDidMount() {
   this.setState({inputValue: this.props.inputValue});
}
handleChange = (e) => {
  this.setState({inputValue: e.target.value});
}

<input value={this.state.inputValue} onChange={this.handlechange} onBlur={() => this.props.actions.updateInput(this.state.inputValue)} />
2020-07-22