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

Riya Kapuria :

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(
    <div className="form-group">
      <input
        value={this.state.fields["name"]}
        onChange={this.onChange.bind(this, "name")}
        className="form-control"
        type="text"
        refs="name"
        placeholder="Name *"
      />
      <span style={{color: "red"}}>{this.state.errors["name"]}</span>
    </div>
  )
}
Mayank Shukla :

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 || '') = ''

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React form error changing a controlled input of type text to be uncontrolled

Warning: A component is changing a controlled input of type undefined to be uncontrolled

ReactJS Warning: TextField is changing an uncontrolled input of type text to be controlled

React a component is changing an uncontrolled input of type checkbox to be controlled

Formik component changing an uncontrolled input of type text to be controlled

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

A component is changing an uncontrolled input of type checkbox to be controlled in React JS

Material UI Select Component- A component is changing a controlled input of type text to be uncontrolled

Email Input Warning - A component is changing a controlled input of type text to be uncontrolled

A component is changing a controlled input of type text to be uncontrolled - ReactJS

React.JS Typescript - OnChange says "A component is changing a controlled input of type text to be uncontrolled in OnChange" for State Object

A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled

Can you help me to get rid of this? Warning: A component is changing an uncontrolled input of type search to be controlled

Uncontrolled input of type text to be controlled warning

Warning: TextField is changing a controlled input of type text to be uncontrolled

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

Uncontrolled input of type text to be controlled In Fomik Feild

ReactJS, A component is changing an uncontrolled input of type number to be controlled

React Input Warning: A component is changing a controlled input of type text to be uncontrolled

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

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

Getting "A component is changing an uncontrolled input to be controlled (...)" when using KeyboardDatePicker

(ReactJS) "Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined ..."

React Formik Warning: A component is changing an uncontrolled input to be controlled

On clearing input label I'm getting a component is changing an uncontrolled input of type text to be controlled

Warning: A component is changing an uncontrolled input to be controlled

'A component is changing a controlled input to be uncontrolled' and partial state update

Warning- A component is changing a controlled input to be uncontrolled

shadcn Input + Form + Zod "A component is changing an uncontrolled input to be controlled"

TOP Ranking

HotTag

Archive