Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
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)
`
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!