Created
May 19, 2021 14:26
-
-
Save codemile/bd2c0ce2b854d46920a8eb8665686b02 to your computer and use it in GitHub Desktop.
Repeater triggers the onRepeat event multiple times at the speed interval.
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
| import {FC, useEffect} from 'react'; | |
| export interface RepeaterProps { | |
| disabled?: boolean; | |
| emitFirst?: boolean; | |
| onRepeat: () => void; | |
| speed: number; | |
| } | |
| export const Repeater: FC<RepeaterProps> = ({ | |
| emitFirst, | |
| disabled, | |
| onRepeat, | |
| speed | |
| }) => { | |
| useEffect(() => { | |
| if (!disabled) { | |
| emitFirst && onRepeat(); | |
| if (speed > 0) { | |
| const id = setInterval(() => onRepeat(), speed); | |
| return () => clearInterval(id); | |
| } | |
| } | |
| }, [emitFirst, disabled, onRepeat, speed]); | |
| return null; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment