一个组件正在将一个不受控制的文本类型输入更改为 ReactJS 中的受控错误 [英] A component is changing an uncontrolled input of type text to be controlled error in ReactJS

查看:26
本文介绍了一个组件正在将一个不受控制的文本类型输入更改为 ReactJS 中的受控错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

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

以下是我的代码:

构造函数(道具){超级(道具);this.state = {字段:{},错误:{}}this.onSubmit = this.onSubmit.bind(this);}....onChange(field, e){让字段 = this.state.fields;字段[字段] = e.target.value;this.setState({fields});}....使成为() {返回(<div className="form-group"><输入值={this.state.fields["name"]}onChange={this.onChange.bind(this, "name")}类名=表单控件"类型=文本"参考=名称"占位符=名称*"/><span style={{color: "red"}}>{this.state.errors["name"]}</span>

)}

解决方案

原因是,在你定义的状态:

this.state = { 字段:{} }

fields 作为一个空白对象,所以在第一次渲染时 this.state.fields.name 将是 undefined,并且输入字段将获得其值:

value={undefined}

因此,输入字段将变得不受控制.

一旦您在 input 中输入任何值,状态中的 fields 将更改为:

this.state = { fields: {name: 'xyz'} }

那时输入字段被转换为受控组件;这就是您收到错误的原因:

<块引用>

一个组件正在将一个不受控制的文本类型输入更改为控制.

可能的解决方案:

1- 将状态中的 fields 定义为:

this.state = { fields: {name: ''} }

2- 或者使用 短路评估 像这样:

value={this.state.fields.name ||''}//(未定义 || '') = ''

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

解决方案

The reason is, in state you defined:

this.state = { fields: {} }

fields as a blank object, so during the first rendering this.state.fields.name will be undefined, and the input field will get its value as:

value={undefined}

Because of that, the input field will become uncontrolled.

Once you enter any value in input, fields in state gets changed to:

this.state = { fields: {name: 'xyz'} }

And at that time the input field gets converted into a controlled component; that's why you are getting the error:

A component is changing an uncontrolled input of type text to be controlled.

Possible Solutions:

1- Define the fields in state as:

this.state = { fields: {name: ''} }

2- Or define the value property by using Short-circuit evaluation like this:

value={this.state.fields.name || ''}   // (undefined || '') = ''

这篇关于一个组件正在将一个不受控制的文本类型输入更改为 ReactJS 中的受控错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆