0

I am new in React development after some practice I was creating a form using react and In console showed some errors, I am seeing this type of error the first time so please can you help me understand this? How I can solve this ? where I did mistake?

import React, { useState } from "react";
    
    const ControlledInputs = () => {
      const [username, setUsername] = useState();
      const [email, setEmail] = useState();
    
      const handleSubmit = (e) => {
        e.preventDefault();
        console.log(username, email);
      };
    
      return (
        <>
          <article>
            <form className="form" onSubmit={handleSubmit}>
              <div className="form-control">
                <label htmlFor="firstName">Username :</label>
                <input
                  type="text"
                  name="username"
                  id="username"
                  value={username}
                  onChange={(e) => setUsername(e.target.value)}
                />
              </div>
              <div className="form-control">
                <label htmlFor="email">Email : </label>
                <input
                  type="text"
                  name="email"
                  id="email"
                  value={email}
                  onChange={(e) => setEmail(e.target.value)}
                />
              </div>
              <button>Add User</button>
            </form>
          </article>
        </>
      );
    };
    
    export default ControlledInputs;

Showing Error in console 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. 
    in input (at 1-controlled-inputs.js:18)
    in div (at 1-controlled-inputs.js:16)
    in form (at 1-controlled-inputs.js:15)
    in article (at 1-controlled-inputs.js:14)
    in ControlledInputs (at App.js:7)
    in div (at App.js:6)
    in App (at src/index.js:8)
    in StrictMode (at src/index.js:7)
`
  • https://stackoverflow.com/questions/47012169/a-component-is-changing-an-uncontrolled-input-of-type-text-to-be-controlled-erro – yochanan sheinberger Dec 06 '21 at 07:08
  • i think this is a duplicated question, but you should add init value for each states – Mohammad Esmaeilzadeh Dec 06 '21 at 07:28
  • 1
    Does this answer your question? [A component is changing an uncontrolled input of type text to be controlled error in ReactJS](https://stackoverflow.com/questions/47012169/a-component-is-changing-an-uncontrolled-input-of-type-text-to-be-controlled-erro) – Ann Dec 06 '21 at 14:53

1 Answers1

0

Just set the initial values of your state variable to an empty string here

 const [username, setUsername] = useState('');
 const [email, setEmail] = useState('');

Explanation: As you did not set the value of username and email. So it is considered undefined. Passing undefined to input value (value={undefined}) makes input uncontrolled. It becomes controlled when you start typing value inside the input. That is why you are getting this warning.

So the best practice is to always set the initial value of your state variables.

Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52