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

116
Visualizações
React saving inputs to state

I have a form that I need to get the information from all the inputs and save it in state.

What am I doing wrong?

const [name,setName] = useState("");

return (
  <div className="App">
    <div class="inputfield">
      <label>Campaign Name</label>
      <input type='text' required maxLength='20' onChange={setName()} className="input"/>
    </div>
  </div>
)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Based on the code that you have provided, it seems that you are not setting your state to anything on change. A good practice would be to establish a handler, that will be triggered when the input's value has been changed - this will set the respective value to your state.

Additionally, since you are effectively making this a controlled input, I would recommend adding the state value as its value attribute. This way, you can handle it directly through the state (say if you wanted to have it reset).

const YourComponent = () => {
 const [name,setName] = useState("");
    
 const nameChangeHandler = (e) => {
    setName(e.target.value)
 };

 return (
 <div className="App">
    <div class="inputfield">
      <label>Campaign Name</label>
      <input type='text' required maxLength='20' onChange={nameChangeHandler} value={name} className="input"/>
   </div> 
 </div>  
};

For more information, you can also check the official documentation.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example of how I set state for input values.

Make sure to set this outside the component (before defining the component).

let initialValues = {
  city: ''
}
const [quote, setQuote] = useState(initialValues)
<input type="text"
  value={ quote.city || "" }
  name="city"
  onChange={ onChange } />
const onChange = (e) => {
  setQuote({...quote, [e.target.name]: e.target.value});
};

you can have as many key values inside the initialValues const. Make sure you define the name of the input along with the value pointing to the state object and an onChange event that handles the setting. Happy Coding!

about 4 years ago · Juan Pablo Isaza Relatório

0

To make a simple change with the code that you have, you can simply rewrite the onChange method for the input element.

onChange={(e) => setName(e.target.value)}
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