forked from sagnik/Project_Velocity
22 lines
686 B
TypeScript
22 lines
686 B
TypeScript
import React from 'react'
|
|
|
|
/**
|
|
* An SSR-friendly useLayoutEffect.
|
|
*
|
|
* React currently throws a warning when using useLayoutEffect on the server.
|
|
* To get around it, we can conditionally useEffect on the server (no-op) and
|
|
* useLayoutEffect elsewhere.
|
|
*
|
|
* @see https://github.com/facebook/react/issues/14927
|
|
*/
|
|
export const useIsomorphicLayoutEffect =
|
|
typeof window !== 'undefined' && (window.document?.createElement || window.navigator?.product === 'ReactNative')
|
|
? React.useLayoutEffect
|
|
: React.useEffect
|
|
|
|
export function useMutableCallback<T>(fn: T) {
|
|
const ref = React.useRef<T>(fn)
|
|
useIsomorphicLayoutEffect(() => void (ref.current = fn), [fn])
|
|
return ref
|
|
}
|