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

Eric Nguyen

In my React app, I'm getting this error during onChange event with my email input field:

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).

Here's the onChange block that's causing this warning; The error goes away if I remove the first if block but of course I need it there for email validation.

validateEmail(email) {
    const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

handleOnChange = e => {
const { name, value } = e.target;
const emailInput = e.target.value;
const emailValid = this.validateEmail(emailInput);

if (name === 'email') {
  this.setState({
    inputs: {
      email: emailInput,
    },
    errors: {
      email: !emailValid,
    },
  });
} else {
  this.setState({
    inputs: {
      ...this.state.inputs,
      [name]: value,
    },
    errors: {
      ...this.state.errors,
      [name]: false,
    },
  });
}
};

State:

constructor() {
super();
this.state = {
  inputs: {
    name: '',
    email: '',
    message: '',
  },
  phone: '',
  show: true,
  errors: {
    name: false,
    email: false,
    message: false,
  },
};
}

How do I keep my current code and address the warning?

Cool Guy

You need to spread the existing/previous state in the if-block. You likely have other input tags that were initially connected to the input-state object which looks like:

inputs: {
   name: "",
   email: "",
   message: ""
}

<input value={this.state.input.name} name="name"/>
<input value={this.state.input.email} name="email"/>
<input value={this.state.input.message} name="message"/>

but when you used this.setState() in your posted code, the connection is lost. You are setting the inputs state to an object with a single property of email:

inputs: {
   email: "valueFromEventTarget"
}

What you need to do is spread the existing state so you don't lose the other key/value pairs in the input object: Update your handleChange() function to this:

handleOnChange = e => {
const { name, value } = e.target;
const emailInput = e.target.value;
const emailValid = this.validateEmail(emailInput);

if (name === 'email') {
  this.setState({
    inputs: {
      ...this.state.inputs,
      email: emailInput,
    },
    errors: {
      ...this.state.errors,
      email: !emailValid,
    },
  });
} else {
  this.setState({
    inputs: {
      ...this.state.inputs,
      [name]: value,
    },
    errors: {
      ...this.state.errors,
      [name]: false,
    },
  });
}
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Warning: 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

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

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

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

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

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

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

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

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

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

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

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

Uncontrolled input of type text to be controlled warning

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

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

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

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

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

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

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

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

Generate input/label element in React child component conditionally but got a 'component is changing an uncontrolled input to be controlled.' warning

Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined,

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

Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined

Uncontrolled input of type text to be controlled In Fomik Feild