From a3c4140751cd271baaf32cd4ec4bf30a4a8d3e37 Mon Sep 17 00:00:00 2001 From: Patrick Silva Date: Tue, 7 Jan 2020 11:38:00 +0100 Subject: [PATCH] fix useIsomorphicLayoutEffect in Node Fix condition to work in Node even when the window object is defined Closes https://github.com/reduxjs/react-redux/issues/1492 --- src/utils/useIsomorphicLayoutEffect.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/utils/useIsomorphicLayoutEffect.js b/src/utils/useIsomorphicLayoutEffect.js index 0e87d6e0c..69e660bd5 100644 --- a/src/utils/useIsomorphicLayoutEffect.js +++ b/src/utils/useIsomorphicLayoutEffect.js @@ -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 @@ -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