Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>   ) } 
like image 567
Riya Kapuria Avatar asked Oct 30 '17 09:10

Riya Kapuria


People also ask

How do you fix a component is changing an uncontrolled input to be controlled?

The warning "A component is changing an uncontrolled input to be controlled" occurs when an input value is initialized to undefined but is later changed to a different value. To fix the warning, initialize the input value to an empty string, e.g. value={message || ''} .

What is an uncontrolled component in React Why would we use it instead of a controlled one?

In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself. To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.

What are uncontrolled components in React JS?

What are uncontrolled components in React? Uncontrolled components are those for which the form data is handled by the DOM itself. “Uncontrolled” refers to the fact that these components are not controlled by React state. The values of the form elements are traditionally controlled by and stored on the DOM.

What is controlled input React?

controlled input is an input that gets its value from a single source of truth. For example the App component below has a single <input> field which is controlled: class App extends React. Component { constructor(props) { super(props); this.


2 Answers

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 || '') = '' 
like image 184
Mayank Shukla Avatar answered Sep 19 '22 03:09

Mayank Shukla


Changing value to defaultValue will resolve it.

Note:

defaultValue is only for the initial load. If you want to initialize the input then you should use defaultValue, but if you want to use state to change the value then you need to use value. Read this for more.

I used value={this.state.input ||""} in input to get rid of that warning.

like image 24
MoFoLuWaSo Avatar answered Sep 21 '22 03:09

MoFoLuWaSo