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

238
Visualizações
Get Value of array name in react js in functional componet

How can I get value with input name array in functional component based react js.

Here is my code

<form method="post" onSubmit={handleSubmit}>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <button className="btn btn-default text-light pos-rel">Verify</button>
 </form>

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should store input values with state, for this i suggest an object.

const [values, setValues] = useState({otp1: 0, otp2: 0, ...restOfTheInputs });

Then you can trigger a change of state when an input is changed by adding a onChange prop to your inputs. (You should also give these inputs a unique name)

<input type="text" name="otp1" value={values.opt1} onChange={(e) => handleInput(e)} />

in this function you will want to update state with the new value for the input replacing any old value while keeping any values from the other inputs.

const handleInput = (event) => {
    const {name, value} = event.target;
    setValues(currentValue => ({...currentValue, ...{name: 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