简体   繁体   English

为什么这个组件从受控切换到不受控制? - 反应

[英]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.

相关问题 为什么我在响应中从受控输入切换到非受控输入 - Why am I switching from controlled to uncontrolled input in react 为什么不建议将 React 组件从受控切换为不受控? - Why is it not recommended to switch a React component from controlled to uncontrolled? 这是受控的还是非受控的 React 组件? - Is this a controlled or uncontrolled React component? 反应错误:从受控制到不受控制的组件 - React error: from controlled to uncontrolled component React 受控组件和非受控组件的区别 - Difference between React controlled component and uncontrolled component 构建一个既可以控制也可以不受控制的React组件 - Building a React Component that can be both Controlled and Uncontrolled 反应:警告,一个组件正在改变一个不受控制的输入来控制 - React: Warning, a component is changing an uncontrolled input to be controlled 我如何使用反应挂钩阻止输入元素从不受控制切换到受控,反之亦然? - How can i stop input elements from switching to from uncontrolled to controlled and vice versa using react hooks? 如何在不受控制/受控制的输入之间切换? 反应 - How am I switching between an uncontrolled/controlled input? React ReactJS 将输入元素从受控状态切换到非受控状态并具有定义的值状态 - ReactJS switching input element from controlled to uncontrolled with defined value state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM