Skip to content

Instantly share code, notes, and snippets.

@codemile
Created May 19, 2021 14:26
Show Gist options
  • Select an option

  • Save codemile/bd2c0ce2b854d46920a8eb8665686b02 to your computer and use it in GitHub Desktop.

Select an option

Save codemile/bd2c0ce2b854d46920a8eb8665686b02 to your computer and use it in GitHub Desktop.
Repeater triggers the onRepeat event multiple times at the speed interval.
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