Skip to content

Commit d6c7d0d

Browse files
fix: useLayoutEffect workaround for ssr
1 parent 33170c0 commit d6c7d0d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Diff for: src/components/Tooltip/Tooltip.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useEffect, useState, useRef, useLayoutEffect } from 'react'
1+
import { useEffect, useState, useRef } from 'react'
22
import classNames from 'classnames'
33
import debounce from 'utils/debounce'
44
import { TooltipContent } from 'components/TooltipContent'
55
import { useTooltip } from 'components/TooltipProvider'
6+
import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect'
67
import { computeTooltipPosition } from '../../utils/compute-positions'
78
import styles from './styles.module.css'
89
import type { IPosition, ITooltip } from './TooltipTypes'
@@ -63,7 +64,7 @@ const Tooltip = ({
6364
* but should be used carefully because of caveats
6465
* https://beta.reactjs.org/reference/react/useLayoutEffect#caveats
6566
*/
66-
useLayoutEffect(() => {
67+
useIsomorphicLayoutEffect(() => {
6768
mounted.current = true
6869
return () => {
6970
mounted.current = false

Diff for: src/utils/use-isomorphic-layout-effect.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { useLayoutEffect, useEffect } from 'react'
2+
3+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect
4+
5+
export default useIsomorphicLayoutEffect

0 commit comments

Comments
 (0)