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

241
Visualizações
How to reach all input field values and submit with button in form?

What I want to do is simple, just take input field values and do something with them by clicking button.

function TableFooterPanel() {

    const [firstName, setFirstName] = useState('');
    const [lastName, setLastName] = useState('');

    const sampleMethod = (x, y) => {
        console.log(x + y)
    }

    return (

        <>
            <Card className='buttonFooter'>
                <Form className='buttonFooter'>
                    <input type="text" placeholder="First Name" defaultValue={firstName} onClick={setFirstName}></input>
                    <input type="text" placeholder="Last Name" defaultValue={lastName} onClick={setLastName}></input>
                    <Button onClick={() => sampleMethod(firstName, lastName)}>Add</Button>
                </Form>
            </Card>

        </>

    );

}
export default TableFooterPanel;

Generally examples for that is done with separate handleChange() methods for each input field but I don't want to make separate functions for each so I though to use useState. How can I call the sampleMethod with these parameters ? I could not update values properly with useState.

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

0

if you use input field as text, you need onChange :

<input type="text" placeholder="First Name" defaultValue={firstName} onChange={e => setFirstName(e.target.value)}></input>

That way the text that you typing in the input will save in the state.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use FormData.

Another suggestion from me is to add an onSubmit event to the form. It's more convenient.

The button should be type="submit". When the user clicks on it the form's onSubmit event will fire.

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