React报错 You provided a `value` prop to a form field without...

报错内容

Failed prop type: You provided a `value` prop to a form field without an `onChange` handler. 
This will render a read-only field. If the field should be mutable use `defaultValue`. 
Otherwise, set either `onChange` or `readOnly`.

<PhoneInput
  key={key}
  label={item.label}
  name={key}
  disabled
  // readOnly
  value={this.state[key]}
  interCodeValue={this.state.intercode}
/>

缘由

缘由是由于input标签,没有定义onChange 可是提供了value属性。React会抛出警告,并将元素设置为只读。ide

若是目标是只读字段,最好使用readOnly属性明确加以定义。这不只会消除警告,也会确保代码的可读性。this

解决:

能够添加readOnly={true} ,或者直接添加readOnly属性,而不设置值,React会默认将该属性的值设为true。spa

相关文章
相关标签/搜索