Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting this error below.

Warning: TextField 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 am using material-ui.

Here is my code:

class RegistrationForm extends React.Component{
constructor(props) {
    super(props)
    this.state = { errorText: '', value:this.props }
  }

  phone(event) {
    var strRegExpMobile=/^\d{10}$/;
    if (event.target.value.match(strRegExpMobile)) {
      this.setState({ errorText: '',
                        phone:event.target.value
                     })
    } else {
      this.setState({ errorText: 'Invalid format' })
    }
  }
  handleSubmit(event){
    alert("submit");
    var data={
        phone:this.state.phone
    }
    console.log(data)
  }
  render() {
    return (
        <div>
          <TextField hintText="Phone"
           floatingLabelText="Phone"
           name="phone"
           value={this.state.phone}
           errorText= {this.state.errorText}
           onChange={this.phone.bind(this)}/>

          <RaisedButton label="Submit" 
           primary={true} 
           onClick={this.handleSubmit.bind(this)}/>
        </div>
    )
  }
}

Can any one tell where I am wrong?

like image 361
Sweety Avatar asked May 22 '17 09:05

Sweety


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 are controlled and uncontrolled inputs 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.

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 are controlled inputs in 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

Reason is, you didn't define the phone in state variable so on initial rendering TextField will be treated as uncontrolled component because value property will have undefined value.

In this line

value = {this.state.phone} => this.state.phone is undefined initially

To fix this define phone in state variable, like this:

constructor(props) {
    super(props)
    this.state = { 
        errorText: '', 
        value:this.props, 
        phone: '' 
    }
}

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

value = {this.state.phone || ''}      //(undefined || '') = ''
like image 62
Mayank Shukla Avatar answered Oct 16 '22 23:10

Mayank Shukla


Because your value is undefined that's why you are getting this error

Try this

render() {
    return (
        <div>
          <TextField hintText="Phone"
           floatingLabelText="Phone"
           name="phone"
           value={this.state.phone || ''}
           errorText= {this.state.errorText}
           onChange={this.phone.bind(this)}/>

          <RaisedButton label="Submit" 
           primary={true} 
           onClick={this.handleSubmit.bind(this)}/>
        </div>
    )
  }
like image 23
Ashh Avatar answered Oct 17 '22 00:10

Ashh