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

以下是我的代码:

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

当前回答

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

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

其他回答

在我的情况下,这几乎是玛雅克·舒克拉的最高答案所说的。唯一的细节是我的州完全没有我所定义的财产。

例如,如果您的状态为:

state = {
    "a" : "A",
    "b" : "B",
}

如果您正在扩展代码,您可能需要添加一个新属性,因此,在代码的其他地方,您可能会创建一个新的属性c,其值不仅在组件的状态中未定义,而且属性本身也未定义。

要解决这个问题,只需确保将c添加到状态中,并给它一个适当的初始值。

例如。,

state = {
    "a" : "A",
    "b" : "B",
    "c" : "C", // added and initialized property!
}

希望我能解释我的边缘案例。

我使用react钩子遇到了同样的警告,尽管我之前已经将初始状态初始化为:-

const[post,setPost]=useState({title:“”,body:“”})

但后来我重写了onChange事件处理程序上预定义状态对象的一部分,

 const onChange=(e)=>{
        setPost({[e.target.name]:e.target.value})
    }

解决方案我通过首先处理前一状态的整个对象(通过使用扩展运算符),然后在其上进行编辑来解决这个问题,

 const onChange=(e)=>{
        setPost({...post,[e.target.name]:e.target.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之后,效果很好。

可以应用多种方法:

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

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' />)

}


如上所述,您需要设置初始状态,在我的例子中,我忘记在setSate()中添加“”引号;

  const AddUser = (props) => {
  const [enteredUsername, setEnteredUsername] = useState()
  const [enteredAge, setEnteredAge] = useState()

出现以下错误

正确的代码是简单地将初始状态设置为空字符串“”

  const AddUser = (props) => {
  const [enteredUsername, setEnteredUsername] = useState('')
  const [enteredAge, setEnteredAge] = useState('')