Skip to content

Commit dadbeb4

Browse files
authored
Merge pull request #187 from pierpo/fix/ssr-warning
fix: fix SSR warning for useLayoutEffect
2 parents 796b0e7 + ab608cf commit dadbeb4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/ArcherElement/ArcherElement.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { useCallback, useContext, useLayoutEffect, useMemo, useRef } from 'react';
1+
import React, { useCallback, useContext, useMemo, useRef } from 'react';
2+
import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect';
23
import { ArcherContainerContext } from '../ArcherContainer/ArcherContainer.context';
34
import { RelationType } from '../types';
45
import { encodeId } from '../utils/encodeId';
@@ -53,7 +54,7 @@ const ArcherElement = ({ id, relations = [], children }: ArcherElementProps) =>
5354
context.unregisterChild(encodedId);
5455
}, [context, encodedId]);
5556

56-
useLayoutEffect(() => {
57+
useIsomorphicLayoutEffect(() => {
5758
registerChild(ref.current);
5859

5960
return () => unregisterChild();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useLayoutEffect, useEffect } from 'react';
2+
3+
/**
4+
* Ignores useLayoutEffect for server-side rendering. It's fine, since the
5+
* server can't draw the arrows.
6+
* https://medium.com/@alexandereardon/uselayouteffect-and-ssr-192986cdcf7a
7+
*/
8+
export const useIsomorphicLayoutEffect =
9+
typeof window !== 'undefined' ? useLayoutEffect : useEffect;

0 commit comments

Comments
 (0)