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

175
Visualizações
Why the function is invoked for every input in React component?

I try to learn react component rendering but the problem is that I have a login page with 2 input field and 1 button as:

function LoginPage() {

    const [username, changeUsername] = useState('');
    const [password, changePassword] = useState('');

    const loginRequest = async (username, password) => {
        let response = await service.loginRequest(username, password);
        console.log(response);
    }

    return (
        <Card hoverable className='transaction-button-card'>
            <h1>Enter username and password</h1>
            <input type="text"
                   placeholder="Username"
                   onChange={e => changeUsername(e.target.value)}
                   value={username}></input>
            <input type="text"
                   placeholder="Password"
                   onChange={e => changePassword(e.target.value)}
                   value={password}></input>
            <Button onClick={loginRequest(username, password)}
                    className='withdraw-deposit-button'>Login/Deposit</Button>
        </Card>
    );
}

export default LoginPage;

When the page is rendered the function loginRequest(username, password) automatically triggered once and for every input characters to input fields are also triggering the same function and sending request for each input char. How can I solve this problem? (I don't want to send request automatically when the page is opened and send request with only with button). I would appreciate if you define the problem.

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

0

` function LoginPage() {

const [username, changeUsername] = useState('');
const [password, changePassword] = useState('');

const loginRequest = async (username, password) => {
    let response = await service.loginRequest(username, password);
    console.log(response);
}

return (
    <Card hoverable className='transaction-button-card'>
        <h1>Enter username and password</h1>
        <input type="text"
               placeholder="Username"
               onChange={e => changeUsername(e.target.value)}
               value={username}></input>
        <input type="text"
               placeholder="Password"
               onChange={e => changePassword(e.target.value)}
               value={password}></input>
        <Button onClick={() => loginRequest(username, password)}
                className='withdraw-deposit-button'>Login/Deposit</Button>
    </Card>
);

}

export default LoginPage; `

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