0
votes

I need your help, I'm trying to use a form from react-strap and for some reason I get this error:

Warning: A component is changing a controlled input of type password 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

this is my code. everything is controlled but I still get the error, can anyone spot my mistake?

const LoginForm = () => {
  const [formData, setFormData] = useState({
    email: " ",
    password: " "
  });
  const onChange = e => {
    setFormData({
      [e.target.name]: e.target.value
    });
  };
  const onSubmit = e => {
    e.preventDefault();
    console.log("email password are", formData.email, formData.password);
    loginUser(formData.email, formData.password);
  };
  return (
    <Form onSubmit={onSubmit}>
      <FormGroup>
        <h1>Login</h1>
      </FormGroup>
      <FormGroup>
        <Label for="Email">Email</Label>
        <Input
          type="email"
          name="email"
          id="Email"
          placeholder="[email protected]"
          onChange={onChange}
          value={formData.email}
        />
      </FormGroup>
      <FormGroup>
        <Label for="Password">Password</Label>
        <Input
          type="password"
          name="password"
          id="Password"
          placeholder="password"
          onChange={onChange}
          value={formData.password}
        />
      </FormGroup>
      <Button>Submit</Button>
    </Form>
  );
};
1

1 Answers

0
votes

Ok I found the error, I didn't spread the last answer, so by inputting password I overwritten my email, the proper on change should be this:

const onChange = e => {
    setFormData({ ...formData, [e.target.name]: e.target.value });
  };