App: add helper hooks
This commit is contained in:
@@ -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
16
src/hooks/useDebounce.ts
Normal 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
37
src/hooks/useThrottle.ts
Normal 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
44
src/hooks/useTimeoutFn.ts
Normal 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
12
src/hooks/useUnmount.ts
Normal 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;
|
||||
Reference in New Issue
Block a user