Controlled inputs with the potential for `undefined` value
See original GitHub issueUpgraded to React 15rc1 today, and a new warning appears to have cropped up:
Warning: SearchBox is changing a uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or viceversa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components
The component it’s referring to is indeed a controlled component for the duration of its lifetime:
<input
type="text"
className="typeahead"
onKeyUp={::this.controlsHandler}
onChange={::this.changeHandler}
value={this.props.search.get('term')}
ref={(i) => this._input = i}
/>
The problem, I’ve realized, is that on first render, this.props.search.get('term')
returns undefined
, because its value is only populated with text put into the input.
I expect this is a pretty common pattern for controlled components?
Otherwise loving the release - having all SVG attributes at my disposal is very exciting 😃
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:17 (1 by maintainers)
Top Results From Across the Web
Getting undefined values from Input Value in ReactJS
I am using the BaseWeb ReactUI framework for the field UI. Need help stuck on this issue. import { FormControl } from 'baseui/form-control' ......
Read more >Trying to understand React docs section on Controlled Input ...
On the React docs page on Forms, it states the following. What are they saying exactly (that you shouldn't ever set value to...
Read more >this is likely caused by the value changing from undefined to a ...
A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined....
Read more >Forms - React
Controlled Input Null Value If you've specified a value but the input is still editable, you may have accidentally set value to...
Read more >Form Controls - ngrx-forms - Read the Docs
The simplest possible form consists of exactly one form control. ... Controls directly support values of type string , number , boolean ,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
How about removing this warning? It really annoying and makes me write unnecessary checks. To my mind it is normal when in flux Store you have some data which is
undefined
While i can see the point, that this might be a source of bugs. I kind of disagree on the first part. The value property is part of the props object. Therefor you can use Object.keys() to figure out if that key has been intentionally added to the props object or if it hasn’t been. The key will exist if you explicitely set the property even if its value is undefined. When going with this approach
<MyInput value={ undefined } />
would be a controlled component and<MyInput />
would be uncontrolled.