Skip to content

Commit 2e34eb1

Browse files
committed
Get around a warning when using useLayoutEffect on the server
1 parent c465771 commit 2e34eb1

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@szhsin/react-menu",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "An accessible, responsive, and customisable React menu library.",
55
"author": "Zheng Song",
66
"license": "MIT",

src/components/MenuList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, {
22
useState,
33
useReducer,
44
useEffect,
5-
useLayoutEffect,
65
useRef,
76
useMemo,
87
useCallback,
@@ -21,7 +20,8 @@ import {
2120
CloseReason,
2221
Keys,
2322
FocusPositions,
24-
HoverIndexActionTypes
23+
HoverIndexActionTypes,
24+
useLayoutEffect
2525
} from '../utils';
2626

2727

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './useActiveState';
55
export * from './useItemState';
66
export * from './useMenuChange';
77
export * from './useMenuState';
8+
export * from './useIsomorphicLayoutEffect';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useLayoutEffect } from 'react'
2+
3+
// Get around a warning when using useLayoutEffect on the server.
4+
// https://github.com/reduxjs/react-redux/blob/b48d087d76f666e1c6c5a9713bbec112a1631841/src/utils/useIsomorphicLayoutEffect.js#L12
5+
// https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
6+
// https://github.com/facebook/react/issues/14927#issuecomment-549457471
7+
8+
const useIsomorphicLayoutEffect =
9+
typeof window !== 'undefined' &&
10+
typeof window.document !== 'undefined' &&
11+
typeof window.document.createElement !== 'undefined'
12+
? useLayoutEffect
13+
: useEffect;
14+
15+
export { useIsomorphicLayoutEffect as useLayoutEffect };

0 commit comments

Comments
 (0)