Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
I have a button in my react app that i only want it to be pressed once

I have implemented this onclick event in it and I want the functions inside the onclick event to run once

    <button className='btn2' onClick={() => {

OpenForm();

      if (IsLoggedIn === true){
        Like();
    }else if(IsLoggedIn === false){
    return;
    }
      }}>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want users to be able to press the button only once and then disable it after it was pressed, you can try this:

import { useState } from 'react'

function MyComponent() { 

    const [disabled, setDisabled] = useState(false);

    const handleClick = () => {
        IsLoggedIn && Like();
        IsLoggedIn && setDisabled(true);
    };

    return (
        <>
            ...
            <button disabled={disabled} onClick={handleClick}>Click me</button>
            ...
        </>
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

What you need to do is keep track of that using state, let's say a hasClicked, the default state will be false and you changed that in the onClick handler you have up there, an example code snippet would be like:

import { useState } from 'react'

const MyComponent = () => {
    const [hasClicked, setHasClicked] = useState(false)
    ...

    const btnClickHandler = () => {
        ...
        setHasClicked(true)
        ...
    }

    return (
        <button disabled={hasClicked} onClick={btnClickHandler}>Click<button/>
    )
}

That way, the button will be disabled once the user clicks on it because disabled will now be pointing to a true hasClicked value.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda