Created
September 14, 2020 11:54
-
-
Save yarastqt/a35261d77d723d14f6d1945dd8130b94 to your computer and use it in GitHub Desktop.
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, | |
DependencyList, | |
useLayoutEffect, | |
useState, | |
useEffect, | |
useMemo, | |
} from 'react' | |
// lib/canUseDOM | |
const canUseDOM = (): boolean => { | |
return ( | |
typeof window !== 'undefined' && | |
typeof window.document !== 'undefined' && | |
typeof window.document.createElement !== 'undefined' | |
) | |
} | |
// lib/useIsomorphicLayoutEffect | |
function useIsomorphicLayoutEffect(fn: EffectCallback, deps: DependencyList): void { | |
return canUseDOM() ? useLayoutEffect(fn, deps) : useEffect(fn, deps) | |
} | |
let isClientEnv = false | |
let id = 0 | |
function generateId(prefix: string) { | |
return `${prefix}${++id}` | |
} | |
export function useUniqId(prefix: string = ''): string | undefined { | |
const idRef = useMemo(() => (isClientEnv ? generateId(prefix) : undefined), []) | |
const [id, setId] = useState(idRef) | |
useIsomorphicLayoutEffect(() => { | |
if (id === undefined) { | |
setId(generateId(prefix)) | |
} | |
}, []) | |
useEffect(() => { | |
if (!isClientEnv) { | |
isClientEnv = true | |
} | |
}, []) | |
return id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage