[英]Why is this component switching from controlled to uncontrolled? - React
Here is my stateless react component: 这是我的无状态反应组件:
export default ({acresMin, update}) => (
<div>
<input
onChange={(e) => update({'acresMin': e.target.value})}
placeholder="10"
type="text"
value={acresMin}/>
</div>
)
When I change the value in the input
, I get this warning
: 当我更改
input
的值时,我收到此warning
:
Warning: StatelessComponent 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.
I am updating with an onChange
and saving the value in the store
which is being populated through the props
. 我正在使用
onChange
更新并保存通过props
填充的store
的值。
What do I not understand about this? 我怎么不理解这个?
Uncontrolled input does not refer to your component directly, but to the input field that is defined in your component. 不受控制的输入不直接引用您的组件,而是引用组件中定义的输入字段。
React differentiates between controlled and uncontrolled components : 反应区分控制和不受控制的部件 :
An
<input>
without a value property is an uncontrolled component没有value属性的
<input>
是一个不受控制的组件
Is your acresMin
property undefined
when you first render the component? 首次渲染组件时,您的
acresMin
属性是否undefined
? This would cause the input to be renderd as an uncontrolled one at first, but as a controlled one later once the property is set. 这将导致输入首先被渲染为不受控制的输入,但是一旦设置了属性,就会将其作为受控制的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.