警告:组件正在更改要控制的文本类型的非受控输入。输入元件不应从非受控切换到受控(反之亦然)。决定在部件的使用寿命内使用受控或非受控输入元件*

以下是我的代码:

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>
  )
}

当前回答

可以应用多种方法:

基于类的方法:使用本地状态并使用默认值定义现有字段:

constructor(props) {
    super(props);
    this.state = {
      value:''
    }
  }
<input type='text'
                  name='firstName'
                  value={this.state.value}
                  className="col-12"
                  onChange={this.onChange}
                  placeholder='Enter First name' />

在功能型组件中使用Hooks React>16.8:

[value, setValue] = useState('');
<input type='text'
                  name='firstName'
                  value={value}
                  className="col-12"
                  onChange={this.onChange}
                  placeholder='Enter First name' />

如果使用propTypes并在HOC组件为函数样式的情况下为propTypes提供默认值。

 HOC.propTypes = {
    value       : PropTypes.string
  }
  HOC.efaultProps = {
    value: ''
  }

function HOC (){

  return (<input type='text'
                  name='firstName'
                  value={this.props.value}
                  className="col-12"
                  onChange={this.onChange}
                  placeholder='Enter First name' />)

}


其他回答

const [name, setName] = useState()

在文本字段中键入后立即生成错误

const [name, setName] = useState('') // <-- by putting in quotes 

将修复此字符串示例上的问题。

即使将undefined设置为上一次渲染时的值(甚至在正确初始化之前),也会出现问题。

问题在于更换

value={value}

with

value={(value==undefined?null:value)}

我也面临同样的问题。在我的例子中,解决方案是我错过了向元素添加“name”属性。

<div className="col-12">
   <label htmlFor="username" className="form-label">Username</label>
        <div className="input-group has-validation">
             <span className="input-group-text">@</span>
                  <input 
                      type="text" 
                      className="form-control" 
                      id="username"
                      placeholder="Username" 
                      required=""
                      value={values.username}
                      onChange={handleChange}
                  />
                  <div className="invalid-feedback">
                      Your username is required.
                  </div>
        </div>
</div>

在我在属性的输入列表中引入name=username之后,效果很好。

在组件内部,按以下方式放置输入框。

<input className="class-name"
              type= "text"
              id="id-123"
              value={ this.state.value || "" }
              name="field-name"
              placeholder="Enter Name"
              />

对我来说,这是一个错误:

<input onChange={onClickUpdateAnswer} value={answer.text}>
  {answer.text}
</input>

如您所见,我已将字符串传递到Input标记的主体中,

Fix:

<input onChange={onClickUpdateAnswer} value={answer.text}></input>