8000 fix(react): allow using `enabled` when using `useQuery().promise` by arvi18 · Pull Request #7 · coderabbit-test/query · GitHub
[go: up one dir, main page]

Skip to content

fix(react): allow using enabled when using useQuery().promise #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
wip
  • Loading branch information
KATT committed Jan 6, 2025
commit b7cf7fc530f45b973b199aaacf7d34acf6ffb805
28 changes: 24 additions & 4 deletions packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ export function useBaseQuery<
useClearResetErrorBoundary(errorResetBoundary)

// this needs to be invoked before creating the Observer because that can create a cache entry
const isNewCacheEntry = !client
.getQueryCache()
.get(defaultedOptions.queryHash)
const cacheEntry = client.getQueryCache().get(defaultedOptions.queryHash)

const [observer] = React.useState(
() =>
Expand All @@ -82,6 +80,7 @@ export function useBaseQuery<
),
)


const result = observer.getOptimisticResult(defaultedOptions)

React.useSyncExternalStore(
Expand Down Expand Up @@ -130,6 +129,7 @@ export function useBaseQuery<
>(defaultedOptions.queryHash),
})
) {
console.log('throwing error')
throw result.error
}

Expand All @@ -138,12 +138,32 @@ export function useBaseQuery<
result,
)

console.log(
'prefetchInRender',
defaultedOptions.experimental_prefetchInRender,
{
willFetch: willFetch(result, isRestoring),
isRestoring: isRestoring,
isLoading: result.isLoading,
isFetching: result.isFetching,
isServer: isServer,
},
)
if (
defaultedOptions.experimental_prefetchInRender &&
!isServer &&
willFetch(result, isRestoring)
) {
const promise = isNewCacheEntry
const cacheEntryState = cacheEntry?.state

const shouldFetch =
!cacheEntryState ||
(cacheEntryState.data === undefined &&
cacheEntryState.status === 'pending' &&
cacheEntryState.fetchStatus === 'idle')

console.log({ shouldFetch })
const promise = shouldFetch
? // Fetch immediately on render in order to ensure `.promise` is resolved even if the component is unmounted
fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
: // subscribe to the "cache promise" so that we can finalize the currentThenable once data comes in
Expand Down
0