App: add helper hooks

This commit is contained in:
rektdeckard
2021-05-30 23:17:33 -04:00
parent 0e50efb5ea
commit 73b66e2e86
5 changed files with 110 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
-moz-user-select: none;
-webkit-user-select: none;
pointer-events: none;
text-transform: uppercase;
}
input.color-input {

16
src/hooks/useDebounce.ts Normal file
View File

@@ -0,0 +1,16 @@
import { DependencyList, useEffect } from "react";
import useTimeoutFn from "./useTimeoutFn";
export type UseDebounceReturn = [() => boolean | null, () => void];
export default function useDebounce(
fn: Function,
ms: number = 0,
deps: DependencyList = []
): UseDebounceReturn {
const [isReady, cancel, reset] = useTimeoutFn(fn, ms);
useEffect(reset, deps);
return [isReady, cancel];
}

37
src/hooks/useThrottle.ts Normal file
View File

@@ -0,0 +1,37 @@
import { useEffect, useRef, useState } from "react";
import useUnmount from "./useUnmount";
const useThrottle = <T>(value: T, ms: number = 200) => {
const [state, setState] = useState<T>(value);
const timeout = useRef<ReturnType<typeof setTimeout>>();
const nextValue = useRef(null) as any;
const hasNextValue = useRef(0) as any;
useEffect(() => {
if (!timeout.current) {
setState(value);
const timeoutCallback = () => {
if (hasNextValue.current) {
hasNextValue.current = false;
setState(nextValue.current);
timeout.current = setTimeout(timeoutCallback, ms);
} else {
timeout.current = undefined;
}
};
timeout.current = setTimeout(timeoutCallback, ms);
} else {
nextValue.current = value;
hasNextValue.current = true;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
useUnmount(() => {
timeout.current && clearTimeout(timeout.current);
});
return state;
};
export default useThrottle;

44
src/hooks/useTimeoutFn.ts Normal file
View File

@@ -0,0 +1,44 @@
import { useCallback, useEffect, useRef } from "react";
export type UseTimeoutFnReturn = [() => boolean | null, () => void, () => void];
export default function useTimeoutFn(
fn: Function,
ms: number = 0
): UseTimeoutFnReturn {
const ready = useRef<boolean | null>(false);
const timeout = useRef<ReturnType<typeof setTimeout>>();
const callback = useRef(fn);
const isReady = useCallback(() => ready.current, []);
const set = useCallback(() => {
ready.current = false;
timeout.current && clearTimeout(timeout.current);
timeout.current = setTimeout(() => {
ready.current = true;
callback.current();
}, ms);
}, [ms]);
const clear = useCallback(() => {
ready.current = null;
timeout.current && clearTimeout(timeout.current);
}, []);
// update ref when function changes
useEffect(() => {
callback.current = fn;
}, [fn]);
// set on mount, clear on unmount
useEffect(() => {
set();
return clear;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ms]);
return [isReady, clear, set];
}

12
src/hooks/useUnmount.ts Normal file
View File

@@ -0,0 +1,12 @@
import { useRef, useEffect } from "react";
const useUnmount = (fn: () => any): void => {
const fnRef = useRef(fn);
// update the ref each render so if it change the newest callback will be invoked
fnRef.current = fn;
useEffect(() => () => fnRef.current(), []);
};
export default useUnmount;