HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavascriptreactCritical

A component is changing an uncontrolled input of type text to be controlled error in ReactJS

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
inputerrorcomponentchangingtextuncontrolledtypecontrolledreactjs

Problem

Warning: A component 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.*

Following is my code:

constructor(props) {
  super(props);
  this.state = {
    fields: {},
    errors: {}
  }
  this.onSubmit = this.onSubmit.bind(this);
}

....

onChange(field, e){
  let fields = this.state.fields;
  fields[field] = e.target.value;
  this.setState({fields});
}

....

render() {
  return(
    
      
      {this.state.errors["name"]}
    
  )
}

Solution

The reason is, in state you defined:

this.state = { fields: {} }


fields as a blank object, so during the first rendering this.state.fields.name will be undefined, and the input field will get its value as:

value={undefined}


Because of that, the input field will become uncontrolled.

Once you enter any value in input, fields in state gets changed to:

this.state = { fields: {name: 'xyz'} }


And at that time the input field gets converted into a controlled component; that's why you are getting the error:


A component is changing an uncontrolled input of type text to be
controlled.

Possible Solutions:

1- Define the fields in state as:

this.state = { fields: {name: ''} }


2- Or define the value property by using Short-circuit evaluation like this:

value={this.state.fields.name || ''}   // (undefined || '') = ''

Code Snippets

this.state = { fields: {} }
value={undefined}
this.state = { fields: {name: 'xyz'} }
this.state = { fields: {name: ''} }
value={this.state.fields.name || ''}   // (undefined || '') = ''

Context

Stack Overflow Q#47012169, score: 1502

Revisions (0)

No revisions yet.