Skip to content

Commit

Permalink
fix useIsomorphicLayoutEffect in Node
Browse files Browse the repository at this point in the history
Fix condition to work in Node even when the window object is defined

Closes reduxjs#1492
  • Loading branch information
patricksmms authored Jan 7, 2020
1 parent 863128e commit a3c4140
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/utils/useIsomorphicLayoutEffect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { useEffect, useLayoutEffect } from 'react'

const isBrowser = typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'

const isNode = typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null

// React currently throws a warning when using useLayoutEffect on the server.
// To get around it, we can conditionally useEffect on the server (no-op) and
// useLayoutEffect in the browser. We need useLayoutEffect to ensure the store
Expand All @@ -10,8 +18,6 @@ import { useEffect, useLayoutEffect } from 'react'
// subscription is created and an inconsistent state may be observed

export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' &&
typeof window.document !== 'undefined' &&
typeof window.document.createElement !== 'undefined'
isBrowser && !isNode
? useLayoutEffect
: useEffect

0 comments on commit a3c4140

Please sign in to comment.