mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
feat: Split string
This commit is contained in:
4
src/hooks/index.ts
Normal file
4
src/hooks/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export {default as useDebounce} from "./useDebounce";
|
||||
export {default as useTimeout} from "./useTimeout";
|
||||
export {default as usePrevious} from "./usePrevious";
|
||||
export {default as useUpdateEffect} from "./useUpdateEffect";
|
38
src/hooks/useDebounce.ts
Normal file
38
src/hooks/useDebounce.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {useCallback, useEffect, useRef} from "react";
|
||||
import _ from "lodash";
|
||||
|
||||
/**
|
||||
* Debounce hook.
|
||||
* @param {T} callback
|
||||
* @param {number} delay
|
||||
* @returns {T}
|
||||
*/
|
||||
function useDebounce<T extends (...args: never[]) => void>(
|
||||
callback: T,
|
||||
delay: number,
|
||||
): T {
|
||||
const callbackRef = useRef<T>(callback);
|
||||
|
||||
// Update the current callback each time it changes.
|
||||
useEffect(() => {
|
||||
callbackRef.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
const debouncedFn = useCallback(
|
||||
_.debounce((...args: never[]) => {
|
||||
callbackRef.current(...args);
|
||||
}, delay),
|
||||
[delay],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// Cleanup function to cancel any pending debounced calls
|
||||
return () => {
|
||||
debouncedFn.cancel();
|
||||
};
|
||||
}, [debouncedFn]);
|
||||
|
||||
return debouncedFn as unknown as T;
|
||||
}
|
||||
|
||||
export default useDebounce;
|
19
src/hooks/usePrevious.ts
Normal file
19
src/hooks/usePrevious.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
/**
|
||||
* The usePrevious function is a custom hook that returns the previous value of a variable.
|
||||
* It takes in a value as a parameter and returns the previous value.
|
||||
*/
|
||||
function usePrevious<T>(value: T): T | undefined {
|
||||
const ref = useRef<T | undefined>();
|
||||
|
||||
// Store current value in ref
|
||||
useEffect(() => {
|
||||
ref.current = value;
|
||||
}, [value]);
|
||||
|
||||
// Return previous value (happens before update in useEffect above)
|
||||
return ref.current;
|
||||
}
|
||||
|
||||
export default usePrevious;
|
30
src/hooks/useTimeout.ts
Normal file
30
src/hooks/useTimeout.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
/**
|
||||
* The useTimeout function is a custom hook that sets a timeout for a given callback function.
|
||||
* It takes in a callback function and a delay time in milliseconds as parameters.
|
||||
* It returns nothing.
|
||||
*/
|
||||
function useTimeout(callback: () => void, delay: number) {
|
||||
const callbackRef = useRef(callback);
|
||||
|
||||
useEffect(() => {
|
||||
callbackRef.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
useEffect(() => {
|
||||
let timer: NodeJS.Timeout | undefined;
|
||||
|
||||
if (delay !== null && callback && typeof callback === "function") {
|
||||
timer = setTimeout(callbackRef.current, delay);
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
};
|
||||
}, [callback, delay]);
|
||||
}
|
||||
|
||||
export default useTimeout;
|
19
src/hooks/useUpdateEffect.ts
Normal file
19
src/hooks/useUpdateEffect.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { DependencyList, EffectCallback, useEffect, useRef } from "react";
|
||||
|
||||
/**
|
||||
* The useUpdateEffect function is a custom hook that behaves like useEffect, but only runs on updates and not on initial mount.
|
||||
* It takes in an effect function and an optional dependency list as parameters.
|
||||
* It returns nothing.
|
||||
*/
|
||||
const useUpdateEffect = (effect: EffectCallback, deps?: DependencyList) => {
|
||||
const isInitialMount = useRef(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (isInitialMount.current) {
|
||||
isInitialMount.current = false;
|
||||
}
|
||||
return effect();
|
||||
}, deps);
|
||||
};
|
||||
|
||||
export default useUpdateEffect;
|
Reference in New Issue
Block a user