Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to change the value of Select input and then i am getting the following warning in the console.

index.js:1446 Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.

But i am updating the state in correct way only , Here is my code

 <Select         value={props.selectedService}         onChange={props.handleSelectChange}         inputProps={{           name: 'selectedService',                     }}    >    <MenuItem value="">      <em>None</em>    </MenuItem>    <MenuItem value="Dry Cleaning">Dry Cleaning</MenuItem>    <MenuItem value="Washing and Ironing">Washing and Ironing</MenuItem>    <MenuItem value="Rolling">Rolling</MenuItem>  </Select> 

And the handleselectchange functionality is here .

handleSelectChange = ({target: {name,value}}) => {    console.log(name);   console.log(value);    this.setState({      serviceRequest: {        selectedService: value      }    });  } 

And state object declaration is below

state = {     open: false,     selectedDate: new Date(),     selectedTime : new Date(),     mailDetails :{       name:"",       email:'',       message:''     },     serviceRequest: {       name: '',       email: '',       mobileNumber:'',       address:'',       landMark:'',       selectedService:''   } }; 

Can anyone please suggest what is the issue?

like image 867
Roster Avatar asked Mar 30 '19 08:03

Roster


People also ask

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 controlled and uncontrolled components in React?

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.

How do you customize a text field in material UI?

To style a React Material UI text field, we can call the makeStyles function with a function that returns an object that specifies the styles. Then we can set the InputProps prop of the TextField to specify the styles of the input element in the text field.


2 Answers

Uncontrolled here means you may be setting the value of the Select component to undefined, this is because value={props.selectedValue} here. In this the props or selectedValue may come null so it turns out to be a uncontrolled component in that.

To solve the warning you can add condition to check null and set default value.

value={props.selectedValue ? props.selectedValue : " "}

Or for easy syntax using nullish coalescing operator (??)

value={props.selectedValue ?? " "}

like image 166
Raj Saraogi Avatar answered Sep 20 '22 17:09

Raj Saraogi


the same of the accepted answer but check for undefined and provide default value with nullish coalescing operator (??)

value={props.selectedValue ?? ""} 
like image 29
saulpalv Avatar answered Sep 17 '22 17:09

saulpalv