This file contains 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
type AbortCallback = (err: any) => void; | |
type SetAbort = (abortCallback: AbortCallback) => void; | |
export const delay = (ms: number, setAbort?: SetAbort) => { | |
return new Promise((resolve, reject) => { | |
const timeout = setTimeout(resolve, ms); | |
setAbort(err => { | |
clearTimeout(timeout); | |
reject(err); | |
}); |
This file contains 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
import { useEffect } from 'react'; | |
export const useOnComponentUnmount = (onUnmountFn: () => void) => { | |
useEffect(() => () => onUnmountFn(), []); | |
}; |
This file contains 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
import { EffectAsyncCallback, useEffectAsync } from './useEffectAsync'; | |
export const useOnComponentMountAsync = (asyncEffect: EffectAsyncCallback) => { | |
useEffectAsync(asyncEffect, []); | |
}; |
This file contains 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
import { EffectCallback, useEffect } from 'react'; | |
export const useOnComponentMount = (onMountFn: EffectCallback) => { | |
useEffect(() => onMountFn(), []); | |
}; |
This file contains 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
import { useRef } from 'react'; | |
export const useObject = <T extends Record<string, any | unknown>>(obj: T | undefined): T => { | |
const safeObj = (obj || {}) as T; | |
const cached = useRef(safeObj); | |
const { current } = cached; | |
if (current === safeObj) return safeObj; | |
const keys = Object.keys({ ...current, ...safeObj }); |
This file contains 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
import { useMemo } from 'react'; | |
export function useMemoDeps<R, D1, D2, D3, D4, D5, D6, D7, D8>( | |
factory: (...deps: [D1, D2, D3, D4, D5, D6, D7, D8]) => R, | |
deps: [D1, D2, D3, D4, D5, D6, D7, D8] | |
): R; | |
export function useMemoDeps<R, D1, D2, D3, D4, D5, D6, D7>( | |
factory: (...deps: [D1, D2, D3, D4, D5, D6, D7]) => R, | |
deps: [D1, D2, D3, D4, D5, D6, D7] | |
): R; |
This file contains 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
import { DependencyList, EffectCallback, useRef } from 'react'; | |
import { useOnComponentUnmount } from './useOnComponentUnmount'; | |
const arrayMatch = (arr1?: DependencyList, arr2?: DependencyList) => { | |
if (!arr1 || !arr2) return false; | |
if (arr1.length !== arr2.length) return false; | |
return arr1.every((_, i) => arr1[i] === arr2[i]); | |
}; | |
export const useImmediateEffect = (effect: EffectCallback, deps?: DependencyList) => { |
This file contains 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
import { DependencyList, useEffect } from 'react'; | |
export type AsyncDestructor = () => void; | |
export type SetDestructor = (setDestructor: AsyncDestructor) => void; | |
export type EffectAsyncCallback = (destructor: SetDestructor) => Promise<void>; | |
export const useEffectAsync = (asyncEffect: EffectAsyncCallback, deps: DependencyList) => { | |
useEffect(() => { | |
let destructor: AsyncDestructor = () => {}; | |
asyncEffect(newDestructor => (destructor = newDestructor)); |
This file contains 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
import { useMemo, useRef } from 'react'; | |
import { useScope } from './useScope'; | |
export type UseDebouncerCallback = (...args: any[]) => void; | |
export const useDebouncer = <T extends UseDebouncerCallback>( | |
callback: T, | |
delay: number, | |
runFirst?: boolean | |
): T & { clear: () => void } => { |
This file contains 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
import { useMemo } from 'react'; | |
export const useConst = <T>(initialStateGetter: () => T) => { | |
return useMemo(initialStateGetter, []); | |
}; |