Skip to content

Instantly share code, notes, and snippets.

@codemile
Created July 7, 2021 11:43
Show Gist options
  • Save codemile/f2a28b9e2f3f4eae54218d5d5ec49ad6 to your computer and use it in GitHub Desktop.
Save codemile/f2a28b9e2f3f4eae54218d5d5ec49ad6 to your computer and use it in GitHub Desktop.
Hook that rotates the items of an Array over time like a news ticker tape.
const useTickerTape = <TType extends any>(
values: Iterable<TType>,
speed: number
) => {
const [tape, setTape] = useState([...values]);
useEffect(() => {
const id = setTimeout(() => {
const first = tape.shift();
setTape([...tape, first]);
}, speed);
return () => clearTimeout(id);
}, [tape, speed]);
return tape;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment