Created
July 7, 2021 11:43
-
-
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.
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
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