Skip to content

Instantly share code, notes, and snippets.

@valakhosravi
Last active January 12, 2024 18:41
Show Gist options
  • Select an option

  • Save valakhosravi/95fbef55c69d3848ee7788c55112e678 to your computer and use it in GitHub Desktop.

Select an option

Save valakhosravi/95fbef55c69d3848ee7788c55112e678 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from "react";
export default function TimerButton({
duration,
action,
}: {
duration: number;
action: () => void;
}) {
const [counter, setCounter] = useState(duration);
useEffect(() => {
if (counter > 0) {
setTimeout(() => {
setCounter(counter - 1);
}, 1000);
}
}, [counter]);
return (
<button
disabled={counter > 0}
onClick={() => {
action();
setCounter(duration);
}}
className={`rounded-[12px] px-[16px] py-[8px] border ${
counter > 0
? "border-black-100 text-black-400"
: "border-primary text-primary"
}`}
>
{counter > 0 ? <span>Try in {counter}sec</span> : <span>Resend</span>}
</button>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment