Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda