简体   繁体   English

反应:警告,一个组件正在改变一个不受控制的输入来控制

[英]React: Warning, a component is changing an uncontrolled input to be controlled

I'm using Google maps address autocomplete in my React app.我在我的 React 应用程序中使用Google 地图地址自动完成功能 It works by hooking into an input element, watching for changes, and providing a dropdown location select.它通过挂钩输入元素、观察变化并提供下拉位置 select 来工作。

Relevant code:相关代码:

<InputGroup hasValidation className="mb-3">
    <FormControl id="autocomplete"/>
</InputGroup>
useEffect(() => {
        
    // only specify the fields we need, to minimize billing
    const options = {"fields": ["address_components"]}

    const autocompleteElement = document.getElementById("autocomplete")

    if(autocompleteElement) {

        autocomplete.current = new google.maps.places.Autocomplete(autocompleteElement, options);
            
        const listener = autocomplete.current.addListener("place_changed", placeSelected);
        return function cleanUp() {
            google.maps.event.clearInstanceListeners(listener);
        }
    } else {
        // nothing to do yet
        return;
    }
});

However, I'm getting a warning in the browser console :但是,我在浏览器控制台中收到警告

Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component

Seems obvious enough- the autocomplete functionality is changing the input itself as opposed to using react state to make a controlled component.似乎很明显 - 自动完成功能正在改变输入本身,而不是使用 react state 来制作受控组件。 However that's the way I want it.然而,这就是我想要的方式。 Is there a way I can silence this error?有没有办法可以消除这个错误? I've tried adding an empty defaultValue and an empty onChange function, but still got the error.我尝试添加一个空的defaultValue和一个空的onChange function,但仍然出现错误。 Thanks in advance!提前致谢!

(There were a few questions with the same issue, but nothing about deliberately disabling the warning) (同样的问题有几个问题,但没有关于故意禁用警告)

Use a regular uncontrolled html input instead of one of the controlled react-bootstrap inputs.使用常规不受控制的 html input代替受控的反应引导输入之一。
You can use a ref to refer to the input.您可以使用ref来引用输入。

<InputGroup hasValidation className="mb-3">
   <input
          defaultValue="Belgium"
          type="text"
          ref={this.autocomplete} />
</InputGroup>

More info on uncontrolled inputs and ref usage here:有关不受控制的输入和ref使用的更多信息:
https://reactjs.org/docs/uncontrolled-components.html https://reactjs.org/docs/uncontrolled-components.html

I have faced such warnings on a couple of projects here is one of the causes and solution.我在几个项目中遇到过这样的警告,这是原因和解决方案之一。

 const [value, setValue] = useState("");
 <input value={value} onChange={inputHandler} />

From the code above notice that the state initial value is "", check yours.从上面的代码注意到 state 的初始值为“”,检查你的。 Its possible you are using null or empty value.您可能正在使用 null 或空值。

You need to change it to empty string and that warning will disappear.您需要将其更改为空字符串,该警告将消失。

Let me know if its helpful.让我知道它是否有帮助。

You can try using a third party custom package like: React Places autocomplete .您可以尝试使用第三方自定义 package ,例如: React Places autocomplete This package provides an option to use controlled input, as well as other props to customise styling and other methods此 package 提供了使用受控输入的选项,以及用于自定义样式和其他方法的其他道具

const [value, setValue] = useState(null);
<GooglePlacesAutocomplete
  selectProps={{
    value,
    onChange: setValue,
  }}
/>

You need to give an initial value and an onChange method.您需要给出一个初始值和一个onChange方法。 I'd probably use a hook to make this easier to manage.我可能会使用一个钩子来使这更容易管理。 You could then use the useEffect() hook to call the API when the value of inputText changes.然后,当inputText的值发生变化时,您可以使用useEffect()挂钩调用 API。

const [inputText, setInputText] = useState('');

useEffect(() => {
    if (inputText.length > 0) {
        ... call the api
    }
},[inputText])

<InputGroup hasValidation className="mb-3">
    <FormControl id="autocomplete" value={inputText} onChange={(event) => {setInputText(event.target.value}}/>
</InputGroup>

as Blessing Ladejobi said its beacuse of正如Blessing Ladejobi所说,这是因为

<input value={myValue} onChange={inputHandler} /> and myValue=null <input value={myValue} onChange={inputHandler} />myValue=null

solution is define a default value for myValue (like myValue="")解决方案是为myValue定义一个默认值(如 myValue="")

You can try this:你可以试试这个:

<InputGroup hasValidation className="mb-3">
    <FormControl id="autocomplete" value={"Canada" || ''}/>
</InputGroup>

This will remove the warning.这将删除警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:组件正在将受控输入更改为反应 js 中的不受控输入 - Warning: A component is changing a controlled input to be uncontrolled input in react js 警告:组件正在将受控输入更改为 React js 中不受控制的输入 - Warning: A component is changing a controlled input to be uncontrolled in React js 电子邮件输入警告-组件将文本类型的受控输入更改为不受控制 - Email Input Warning - A component is changing a controlled input of type text to be uncontrolled 警告:组件正在更改要控制的文本类型的不受控制的输入 - Warning: A component is changing an uncontrolled input of type text to be controlled 警告:组件正在将未定义类型的非受控输入更改为受控 - Warning: A component is changing an uncontrolled input of type undefined to be controlled 警告:组件正在更改要控制的文本类型的不受控输入 - Warning: A component is changing an uncontrolled input of type text to be controlled formik 警告,组件正在更改不受控制的文本类型输入以进行控制 - formik warning, A component is changing an uncontrolled input of type text to be controlled ReactJS - 警告:组件正在更改要控制的文本类型的不受控制的输入 - ReactJS - Warning: A component is changing an uncontrolled input of type text to be controlled 警告:组件正在将文本类型的受控输入更改为不受控制。 (React.js) - Warning: A component is changing a controlled input of type text to be uncontrolled. (React.js) React - 一个组件正在将文本类型的受控输入更改为不受控制 - React - A component is changing a controlled input of type text to be uncontrolled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM