Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!