question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Controlled inputs with the potential for `undefined` value

See original GitHub issue

Upgraded 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:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

59reactions
desercommented, May 11, 2016

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

13reactions
cdxOocommented, Oct 13, 2016

React has no way of knowing if you intended to render a component with an empty value or if you intended for the component to be uncontrolled. It is a source of bugs.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found