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

280
Views
React: button with spinner, how to know when the animation should stop?

I would like to create a custom button component that shows a loading spinner within itself when it's pressed and with a condition that can be externally defined which will tell the button to remove the spinner and return to its original appearance. Something like this:

<CustomButton
  type="button"
  className="btn btn-primary"
  stopSpinningWhen={condition}
  onClick={() => ...}
>
  Click me
</CustomButton>

Currently, my buttons with a spinner look like this, which is super fine, but it's a pain to write repetitive code/states for each single button:

const [buttonSpinner, setButtonSpinner] = useState(false);

const onClickEvent = (ev) => {
  setButtonSpinner(true);

  if (condition) {
    setButtonSpinner(false);
  }
};

return (
  <button
    type="button"
    className="btn btn-primary"
    onClick={onClickEvent}
    disabled={buttonSpinner}
  >
    {buttonSpinner ? (
      <span
        className="spinner-border spinner-border-sm"
        role="status"
        aria-hidden="true"
      ></span>
    ) : (
      "Click me"
    )}
  </button>
);

I'm using React 17.0.2.

Is it even possible?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can create your own custom button that receives isLoading additionally.

const Spinner = (
  <span
    className="spinner-border spinner-border-sm"
    role="status"
    aria-hidden="true"
  />
)

const CustomButton = (props) => (
  <button
    type="button"
    className="btn btn-primary"
    onClick={props.onClick}
    disabled={props.isLoading}
  >
    {props.isLoading ? <Spinner /> : "Click me"}
  </button>
)

const YourComponent = () => {
  const [isLoading, setIsLoading] = useState(false)
  const onClick = async (event) => {
    setIsLoading(true)
    doHeavyTask()
    setIsLoading(false)
  }
  return (
    <div>
      <CustomButton isLoading={isLoading} onClick={onClick} />
    </div>
  )
}
about 4 years ago · Juan Pablo Isaza Report

0

You use a loading property as your condition and just pass that as a prop to your custom button component. Something like this:

const myComponent = () =>{

const [loading, setLoading] = useState(false)

const myFunc = async () = {
 setLoading(true)
 //call api or do seomthing, after the process finishes set loading to false again
 const resp = await fetch("myAPIURL")
 console.log(resp.data)
 setLoading(false)
}

return(

<CustomButton
  type="button"
  className="btn btn-primary"
  spinning={loading}
  onClick={() => myFunc()}
>
  Click me
</CustomButton>
)

}
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!