Skip to content

Commit 68fe131

Browse files
authored
Merge pull request #4436 from aryaemami59/fix-useIsomorphicLayoutEffect
2 parents ff65194 + 7f8091e commit 68fe131

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

Diff for: packages/toolkit/src/query/react/buildHooks.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,27 @@ import { useStableQueryArgs } from './useSerializedStableValue'
5353
import { useShallowStableValue } from './useShallowStableValue'
5454

5555
// Copy-pasted from React-Redux
56+
const canUseDOM = () =>
57+
!!(
58+
typeof window !== 'undefined' &&
59+
typeof window.document !== 'undefined' &&
60+
typeof window.document.createElement !== 'undefined'
61+
)
62+
63+
const isDOM = /* @__PURE__ */ canUseDOM()
64+
65+
// Under React Native, we know that we always want to use useLayoutEffect
66+
67+
const isRunningInReactNative = () =>
68+
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
69+
70+
const isReactNative = /* @__PURE__ */ isRunningInReactNative()
71+
72+
const getUseIsomorphicLayoutEffect = () =>
73+
isDOM || isReactNative ? useLayoutEffect : useEffect
74+
5675
export const useIsomorphicLayoutEffect =
57-
typeof window !== 'undefined' &&
58-
!!window.document &&
59-
!!window.document.createElement
60-
? useLayoutEffect
61-
: useEffect
76+
/* @__PURE__ */ getUseIsomorphicLayoutEffect()
6277

6378
export interface QueryHooks<
6479
Definition extends QueryDefinition<any, any, any, any, any>,
@@ -690,7 +705,10 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
690705
// isFetching = true any time a request is in flight
691706
const isFetching = currentState.isLoading
692707
// isLoading = true only when loading while no data is present yet (initial load with no data in the cache)
693-
const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching
708+
const isLoading =
709+
(!lastResult || lastResult.isLoading || lastResult.isUninitialized) &&
710+
!hasData &&
711+
isFetching
694712
// isSuccess = true when data is present
695713
const isSuccess = currentState.isSuccess || (isFetching && hasData)
696714

0 commit comments

Comments
 (0)