You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
895 B

3 months ago
import * as React from 'react';
// allow the hook to work in SSR
const useLayoutEffect = typeof window !== 'undefined'
? React.useLayoutEffect
: React.useEffect;
/**
* Alternative to useCallback that doesn't update the callback when dependencies change
*
* @see https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback
* @see https://github.com/facebook/react/issues/14099#issuecomment-440013892
*/
export function useInvariant<
Args extends unknown[],
Return>(fn: (...args: Args) => Return,
): ((...args: Args) => Return) {
const ref = React.useRef<(...args: Args) => Return>(() => {
throw new Error('Cannot call an event handler while rendering.');
});
useLayoutEffect(() => {
ref.current = fn;
});
return React.useCallback((...args: Args) => {
return ref.current(...args);
}, []);
}
export default useInvariant;