A component is changing an uncontrolled input of type text to be controlled error in ReactJS

问题:

Warning: A component is changing an uncontrolled input of type text to be controlled.警告:组件正在更改要控制的文本类型的不受控制的输入。 Input elements should not switch from uncontrolled to controlled (or vice versa).输入元素不应从不受控制切换到受控制(反之亦然)。 Decide between using a controlled or uncontrolled input element for the lifetime of the component.*决定在组件的生命周期内使用受控或非受控输入元素。*

Following is my code:以下是我的代码:

constructor(props) {
  super(props);
  this.state = {
    fields: {},
    errors: {}
  }
  this.onSubmit = this.onSubmit.bind(this);
}

....

onChange(field, e){
  let fields = this.state.fields;
  fields[field] = e.target.value;
  this.setState({fields});
}

....

render() {
  return(
    <div className="form-group">
      <input
        value={this.state.fields["name"]}
        onChange={this.onChange.bind(this, "name")}
        className="form-control"
        type="text"
        refs="name"
        placeholder="Name *"
      />
      <span style={{color: "red"}}>{this.state.errors["name"]}</span>
    </div>
  )
}

解决方案:

参考一: https://en.stackoom.com/question/3BG0n
参考二: https://stackoom.com/question/3BG0n
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章