Skip to content

Commit 8c366b2

Browse files
authored
Merge pull request #35 from mufasa71/fix-ssr-layout-warning
Fix useLayoutEffect on SSR
2 parents b5a9c1a + a23e912 commit 8c366b2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/useEventCallback.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {useRef, useCallback, useLayoutEffect} from 'react';
1+
import {useRef, useCallback} from 'react';
2+
import {useIsomorphicLayoutEffect} from './useIsomorphicLayoutEffect';
23

34
export default function useEventCallback(fn: Function) {
45
const ref = useRef<Function>(() => {
56
throw new Error('Function is called before it was assigned.');
67
});
78

8-
useLayoutEffect(() => {
9+
useIsomorphicLayoutEffect(() => {
910
ref.current = fn;
1011
});
1112

src/useIsomorphicLayoutEffect.ts

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

0 commit comments

Comments
 (0)