From 5ba0d93fb2716e0965afbb8cc700963d3e05e02d Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Wed, 10 Apr 2024 01:39:59 -0500 Subject: [PATCH 1/3] Fix `useIsomorphicLayoutEffect` in React Native environments - React Native was not using `useLayoutEffect` which was causing nested component updates to not get properly batched when using the `connect` API [#2150](https://github.com/reduxjs/react-redux/issues/2150). --- src/utils/useIsomorphicLayoutEffect.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/utils/useIsomorphicLayoutEffect.ts b/src/utils/useIsomorphicLayoutEffect.ts index 329c00e10..72a53ac18 100644 --- a/src/utils/useIsomorphicLayoutEffect.ts +++ b/src/utils/useIsomorphicLayoutEffect.ts @@ -16,6 +16,18 @@ export const canUseDOM = !!( typeof window.document.createElement !== 'undefined' ) +// Under React Native, we know that we always want to use useLayoutEffect + +/** + * Checks if the code is running in a React Native environment. + * + * @see {@link https://github.com/facebook/react-native/issues/1331 Reference} + */ +export const isReactNative = + typeof navigator !== 'undefined' && navigator.product === 'ReactNative' + export const useIsomorphicLayoutEffect = canUseDOM ? React.useLayoutEffect - : React.useEffect + : isReactNative + ? React.useLayoutEffect + : React.useEffect From 691b1a2ad3bc35d73cbacfda6e263d3929bb56af Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Wed, 10 Apr 2024 09:07:56 -0500 Subject: [PATCH 2/3] Remove nested ternary --- src/utils/useIsomorphicLayoutEffect.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/utils/useIsomorphicLayoutEffect.ts b/src/utils/useIsomorphicLayoutEffect.ts index 72a53ac18..7a57bd6fb 100644 --- a/src/utils/useIsomorphicLayoutEffect.ts +++ b/src/utils/useIsomorphicLayoutEffect.ts @@ -26,8 +26,5 @@ export const canUseDOM = !!( export const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative' -export const useIsomorphicLayoutEffect = canUseDOM - ? React.useLayoutEffect - : isReactNative - ? React.useLayoutEffect - : React.useEffect +export const useIsomorphicLayoutEffect = + canUseDOM || isReactNative ? React.useLayoutEffect : React.useEffect From fd2c5c61755ded1f29b6bbda9da772cedda57021 Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Wed, 10 Apr 2024 09:10:39 -0500 Subject: [PATCH 3/3] Remove `useIsomorphicLayoutEffect.native.ts` - Since we bundle the library now, and React Native explicitly uses `useLayoutEffect`, we do not need this file anymore. --- src/utils/useIsomorphicLayoutEffect.native.ts | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 src/utils/useIsomorphicLayoutEffect.native.ts diff --git a/src/utils/useIsomorphicLayoutEffect.native.ts b/src/utils/useIsomorphicLayoutEffect.native.ts deleted file mode 100644 index c3b22c296..000000000 --- a/src/utils/useIsomorphicLayoutEffect.native.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { React } from '../utils/react' - -// Under React Native, we know that we always want to use useLayoutEffect - -export const useIsomorphicLayoutEffect = React.useLayoutEffect