Skip to content

Commit c838d4c

Browse files
committed
Convert isReactNative and canUseDOM to functions with @__PURE__s
1 parent 4e2479b commit c838d4c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/utils/useIsomorphicLayoutEffect.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import { React } from '../utils/react'
1010
// subscription is created and an inconsistent state may be observed
1111

1212
// Matches logic in React's `shared/ExecutionEnvironment` file
13-
export const canUseDOM = !!(
14-
typeof window !== 'undefined' &&
15-
typeof window.document !== 'undefined' &&
16-
typeof window.document.createElement !== 'undefined'
17-
)
13+
const canUseDOM = () =>
14+
!!(
15+
typeof window !== 'undefined' &&
16+
typeof window.document !== 'undefined' &&
17+
typeof window.document.createElement !== 'undefined'
18+
)
19+
20+
const isDOM = /* @__PURE__ */ canUseDOM()
1821

1922
// Under React Native, we know that we always want to use useLayoutEffect
2023

@@ -23,11 +26,13 @@ export const canUseDOM = !!(
2326
*
2427
* @see {@link https://github.com/facebook/react-native/issues/1331 Reference}
2528
*/
26-
export const isReactNative =
29+
export const isRunningInReactNative = () =>
2730
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
2831

32+
const isReactNative = /* @__PURE__ */ isRunningInReactNative()
33+
2934
const getUseIsomorphicLayoutEffect = () =>
30-
canUseDOM || isReactNative ? React.useLayoutEffect : React.useEffect
35+
isDOM || isReactNative ? React.useLayoutEffect : React.useEffect
3136

3237
export const useIsomorphicLayoutEffect =
3338
/* @__PURE__ */ getUseIsomorphicLayoutEffect()

0 commit comments

Comments
 (0)