反应输入类型不可编辑 [英] React Input Type not editable

查看:36
本文介绍了反应输入类型不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 react 中向输入文件添加动态值,然后我尝试对其进行编辑,但它根本无法编辑.

I was adding dynamic values to the input file in react then I tried to edit that but it not at all editable.

var shop_profile_data = this.state.data.DETAILS;

<input id="shopname" className="inputMaterial"  value={shop_profile_data.NAME} type="text" required/>

请给我解决方案.谢谢

推荐答案

由于 value 始终以相同的值 (shop_profile_data.NAME) 呈现,因此无法更改.通过设置 value 属性,您使 input 成为受控组件.

Since value is always rendered with the same value (shop_profile_data.NAME) nothing is able to change. By setting value property you are making input a Controlled Component.

您需要添加一个 onChange 事件,然后将 shop_profile_data.NAME 设置为不同的值.然后 input 的值会改变.

You need to add an onChange event and then set the shop_profile_data.NAME to a different value. Then the value of the input will change.

如果您只想设置 input 的初始值,请使用 defaultValue 属性(docs).defaultValue 将设置初始值,但随后允许更改该值.

If you only want to set the initial value of the input, use defaultValue property (docs). defaultValue will set the initial value, but then allow the value to be changed.

有关受控组件与非受控组件的更多信息.

这篇关于反应输入类型不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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