mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-29 00:20:04 +01:00
30 lines
711 B
TypeScript
30 lines
711 B
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
|
|
/**
|
|
* A hook that can be used to rotate values in an array.
|
|
* It will return random item in an array after given interval
|
|
*/
|
|
function useRotator<T>(data: T[], interval = 3000): T | null {
|
|
if (!Array.isArray(data)) return null;
|
|
//@ts-ignore
|
|
const [current, setCurrent] = useState<T>(data.sample());
|
|
const intervalRef = useRef<NodeJS.Timer>();
|
|
|
|
useEffect(() => {
|
|
intervalRef.current = setInterval(() => {
|
|
//@ts-ignore
|
|
setCurrent(data.sample());
|
|
}, interval);
|
|
|
|
return () => {
|
|
if (intervalRef.current) {
|
|
clearInterval(intervalRef.current);
|
|
}
|
|
};
|
|
});
|
|
|
|
return current;
|
|
}
|
|
|
|
export default useRotator;
|