Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda