简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM