Created
August 31, 2022 16:09
-
-
Save wbarcovsky/37aa0f5d8d4041b20f69178eb58de665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const Timer = (props) => { | |
| const [counter, setCounter] = useState(15); | |
| const text = props.text.toUpperCase(); | |
| const handler = React.useRef<any>(); | |
| useEffect(() => { | |
| handler.current = setInterval(() => { | |
| setCounter((prev) => prev - 1); | |
| }, 1000); | |
| return () => clearInterval(handler.current); | |
| }, []); | |
| useEffect(() => { | |
| if (counter === 0) { | |
| clearInterval(handler.current); | |
| } | |
| }); | |
| return ( | |
| <div className="timer"> | |
| <div className="counter" style={{border: '1px solid black'}}> | |
| <div className="text">{text}</div> | |
| <div className="timer_counter">{counter}</div> | |
| </div> | |
| </div> | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment