脚本宝典收集整理的这篇文章主要介绍了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会抛出警告,并将元素设置为只读。
如果目标是只读字段,最好使用readOnly属性明确加以定义。这不仅会消除警告,也会确保代码的可读性。
解决:
可以添加readOnly={true} ,或者直接添加readOnly属性,而不设置值,React会默认将该属性的值设为true。
以上是脚本宝典为你收集整理的React报错 You provided a `value` prop to a form field without...全部内容,希望文章能够帮你解决React报错 You provided a `value` prop to a form field without...所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。