javascript – React – changing an uncontrolled input

The Question :

392 people think this question is useful

I have a simple react component with the form which I believe to have one controlled input:

import React from 'react';

export default class MyForm extends React.Component {
    constructor(props) {
        this.state = {}

    render() {
        return (
            <form className="add-support-staff-form">
                <input name="name" type="text" value={} onChange={this.onFieldChange('name').bind(this)}/>

    onFieldChange(fieldName) {
        return function (event) {

export default MyForm;

When I run my application I get the following warning:

Warning: MyForm 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 believe my input is controlled since it has a value. I am wondering what am I doing wrong?

I am using React 15.1.0

The Question Comments :

The Answer 1

543 people think this answer is useful

I believe my input is controlled since it has a value.

For an input to be controlled, its value must correspond to that of a state variable.

That condition is not initially met in your example because is not initially set. Therefore, the input is initially uncontrolled. Once the onChange handler is triggered for the first time, gets set. At that point, the above condition is satisfied and the input is considered to be controlled. This transition from uncontrolled to controlled produces the error seen above.

By initializing in the constructor:


this.state = { name: '' };

the input will be controlled from the start, fixing the issue. See React Controlled Components for more examples.

Unrelated to this error, you should only have one default export. Your code above has two.

The Answer 2

139 people think this answer is useful

When you first render your component, isn’t set, so it evaluates to undefined or null, and you end up passing value={undefined} or value={null}to your input.

When ReactDOM checks to see if a field is controlled, it checks to see if value != null (note that it’s !=, not !==), and since undefined == null in JavaScript, it decides that it’s uncontrolled.

So, when onFieldChange() is called, is set to a string value, your input goes from being uncontrolled to being controlled.

If you do this.state = {name: ''} in your constructor, because '' != null, your input will have a value the whole time, and that message will go away.

The Answer 3

54 people think this answer is useful

Another approach it could be setting the default value inside your input, like this:

 <input name="name" type="text" value={ || ''} onChange={this.onFieldChange('name').bind(this)}/>

The Answer 4

19 people think this answer is useful

I know others have answered this already. But a very important factor here that may help other people experiencing similar issue:

You must have an onChange handler added in your input field (e.g. textField, checkbox, radio, etc). Always handle activity through the onChange handler.


<input ... onChange={ this.myChangeHandler} ... />

When you are working with checkbox you may need to handle its checked state with !!.


<input type="checkbox" checked={!!this.state.someValue} onChange={.....} >


The Answer 5

14 people think this answer is useful

Simple solution to resolve this problem is to set an empty value by default :

<input name='myInput' value={this.state.myInput || ''} onChange={this.handleChange} />

The Answer 6

7 people think this answer is useful

One potential downside with setting the field value to “” (empty string) in the constructor is if the field is an optional field and is left unedited. Unless you do some massaging before posting your form, the field will be persisted to your data storage as an empty string instead of NULL.

This alternative will avoid empty strings:

constructor(props) {
    this.state = {
        name: null


<input name="name" type="text" value={ || ''}/>

The Answer 7

7 people think this answer is useful

In my case, I was missing something really trivial.

<input value={state.myObject.inputValue} />

My state was the following when I was getting the warning:

state = {
   myObject: undefined

By alternating my state to reference the input of my value, my issue was solved:

state = {
   myObject: {
      inputValue: ''

The Answer 8

6 people think this answer is useful

When you use onChange={this.onFieldChange('name').bind(this)} in your input you must declare your state empty string as a value of property field.

incorrect way:

this.state ={
       fields: {},
       errors: {},
       disabled : false

correct way:

this.state ={
       fields: {
         email: '',
         message: ''
       errors: {},
       disabled : false

The Answer 9

4 people think this answer is useful

Set a value to ‘name’ property in initial state.

this.state={ name:''};

The Answer 10

3 people think this answer is useful

If the props on your component was passed as a state, put a default value for your input tags

<input type="text" placeholder={} value={ ? : ""}>

The Answer 11

3 people think this answer is useful

An update for this. For React Hooks use const [name, setName] = useState(" ")

The Answer 12

1 people think this answer is useful

This generally happens only when you are not controlling the value of the filed when the application started and after some event or some function fired or the state changed, you are now trying to control the value in input field.

This transition of not having control over the input and then having control over it is what causes the issue to happen in the first place.

The best way to avoid this is by declaring some value for the input in the constructor of the component. So that the input element has value from the start of the application.

The Answer 13

0 people think this answer is useful

For dynamically setting state properties for form inputs and keeping them controlled you could do something like this:

const inputs = [
    { name: 'email', type: 'email', placeholder: "Enter your email"},
    { name: 'password', type: 'password', placeholder: "Enter your password"},
    { name: 'passwordConfirm', type: 'password', placeholder: "Confirm your password"},

class Form extends Component {
    this.state = {} // Notice no explicit state is set in the constructor

  handleChange = (e) => {
    const { name, value } =;

      [name]: value

  handleSubmit = (e) => {
    // do something

  render() {
     <form onSubmit={(e) => handleSubmit(e)}>
       { inputs.length ? => {
           const { name, placeholder, type } = input;
           const value = this.state[name] || ''; // Does it exist? If so use it, if not use an empty string

           return <input key={name}  type={type} name={name} placeholder={placeholder} value={value} onChange={this.handleChange}/>
       }) :
       <button type="submit" onClick={(e) => e.preventDefault }>Submit</button>

The Answer 14

-2 people think this answer is useful

Simply create a fallback to ” if the is null.

<input name="name" type="text" value={ || ''} onChange={this.onFieldChange('name').bind(this)}/>

This also works with the useState variables.

Add a Comment