fix(react-query): move away of uSES #8434
main fromrefs/pull/8434/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
github-actions[bot](GitHub profile, opens in a new tab) removed labelspackage: query-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query-devtools
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query-persist-client
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
- packages/react-query/src/__tests__/useIsFetching.test.tsx line 57Outdated@@ -54,4 +54,3 @@5454 await findByText('isFetching: 0')5555 })565657- it('should not update state while rendering', async () => {
- @@ -12,12 +12,11 @@1212 const client = useQueryClient(queryClient)1313 const queryCache = client.getQueryCache()141415- return React.useSyncExternalStore(16- React.useCallback(17- (onStoreChange) =>18- queryCache.subscribe(notifyManager.batchCalls(onStoreChange)),19- [queryCache],20- ),21- () => client.isFetching(filters),22- () => client.isFetching(filters),15+ const [number, setNumber] = React.useState<number>(() =>16+ client.isFetching(filters),2317 )18+19+ const filtersRef = React.useRef(filters)20+ React.useEffect(() => {21+ filtersRef.current = filters22+ }, [filters])
- @@ -85,19 +85,2 @@85- const result = observer.getOptimisticResult(defaultedOptions)86-87- React.useSyncExternalStore(88- React.useCallback(89- (onStoreChange) => {90- const unsubscribe = isRestoring91- ? noop92- : observer.subscribe(notifyManager.batchCalls(onStoreChange))93-94- // Update result to make sure we did not miss any query updates95- // between creating the observer and subscribing to it.96- observer.updateResult()97-98- return unsubscribe99- },100- [observer, isRestoring],101- ),102- () => observer.getCurrentResult(),103- () => observer.getCurrentResult(),85+ const [result, setResult] = React.useState(() =>86+ observer.getOptimisticResult(defaultedOptions),
- @@ -609,3 +610,8 @@609610 * Create a new thenable and result promise when the results have changed610611 */611612 const recreateThenable = () => {613+ console.log(614+ '-----------recreateThenable()',615+ 'queryHash:',616+ query.queryHash,617+ )
- @@ -698,8 +704,10 @@698704 includedProps.add('error')699705 }700706701- return Object.keys(this.#currentResult).some((key) => {707+ return [...includedProps].some((key) => {702708 const typedKey = key as keyof QueryObserverResult703709 const changed = this.#currentResult[typedKey] !== prevResult[typedKey]704-705- return changed && includedProps.has(typedKey)710+ console.log(`changed key "${typedKey}"?`, changed, {711+ prev: this.#currentResult[typedKey],712+ next: prevResult[typedKey],713+ })
- Resolvedby TkDodo@@ -698,4 +704,4 @@698704 includedProps.add('error')699705 }700706701- return Object.keys(this.#currentResult).some((key) => {707+ return [...includedProps].some((key) => {
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-query-next-experimental
github-actions[bot](GitHub profile, opens in a new tab) removed labelspackage: react-query-next-experimental
- packages/react-query/src/__tests__/useQuery.test.tsx line 2488Outdated@@ -2485,4 +2486,3 @@24852486 expect(queryCache.find({ queryKey: key })!.options.retryDelay).toBe(20)24862487 })248724882488- it('should batch re-renders', async () => {
github-actions[bot](GitHub profile, opens in a new tab) removed labelspackage: react-query-devtools
github-actions[bot](GitHub profile, opens in a new tab) removed labelspackage: react-query-persist-client
TkDodo reviewed
@@ -92,13 +89,8 @@92- : observer.subscribe(notifyManager.batchCalls(onStoreChange))93-94- // Update result to make sure we did not miss any query updates95- // between creating the observer and subscribing to it.96- observer.updateResult()97-98- return unsubscribe99- },100- [observer, isRestoring],101- ),102- () => observer.getCurrentResult(),103- () => observer.getCurrentResult(),104- )89+ React.useEffect(() => {90+ if (isRestoring) {91+ return92+ }93+94+ const unsubscribe = observer.subscribe(95+ notifyManager.batchCalls(() => {96+ setForceUpdate((n) => n + 1)packages/react-query/src/useMutationState.ts line 59Outdated@@ -62,15 +54,6 @@62- mutationCache.subscribe(() => {63- const nextResult = replaceEqualDeep(64- result.current,65- getResult(mutationCache, optionsRef.current),66- )67- if (result.current !== nextResult) {68- result.current = nextResult69- notifyManager.schedule(onStoreChange)70- }71- }),72- [mutationCache],73- ),74- () => result.current,75- () => result.current,76- )!54+ const [result, setResult] = React.useState<Array<TResult>>(() =>55+ getResult(mutationCache, options),56+ )57+58+ React.useEffect(() => {59+ const sub = mutationCache.subscribe(() => {- @@ -0,0 +1,11 @@1+import { afterAll, beforeAll, describe, expect, it } from 'vitest'2+import * as React from 'react'3+import {4+ createRenderStream,5+ useTrackRenders,6+} from '@testing-library/react-render-stream'7+import { QueryClientProvider, useQuery } from '..'8+import { QueryCache } from '../index'9+import { createQueryClient, queryKey, sleep } from './utils'10+11+describe('react transitions', () => {
- packages/react-query/src/__tests__/transition.test.tsx lines 15 to 17@@ -0,0 +1,17 @@1+/* eslint-disable @typescript-eslint/require-await */2+import { act, render, screen } from '@testing-library/react'3+import * as React from 'react'4+import { afterAll, beforeAll, expect, it, vi } from 'vitest'5+import { QueryClientProvider, useQuery } from '..'6+import { QueryCache } from '../index'7+import { createQueryClient, queryKey, sleep } from './utils'8+9+const queryCache = new QueryCache()10+const queryClient = createQueryClient({11+ queryCache,12+})13+14+beforeAll(() => {15+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment16+ // @ts-ignore17+ global.IS_REACT_ACT_ENVIRONMENT = true
- .vscode/settings.json lines 1 to 4@@ -0,0 +1,4 @@1+{2+ "editor.defaultFormatter": "esbenp.prettier-vscode",3+ "editor.formatOnSave": true4+}
- packages/react-query/src/useBaseQuery.ts line 120@@ -105,7 +99,21 @@99+100+ // Update result to make sure we did not miss any query updates101+ // between creating the observer and subscribing to it.102+ observer.updateResult()103+104+ return unsubscribe105+ }, [observer, isRestoring])105106106107 React.useEffect(() => {108+ if (defaultedOptions.experimental_prefetchInRender) {109+ return110+ }107111 // Do not notify on updates because of changes in the options because108112 // these changes should already be reflected in the optimistic result.109113 observer.setOptions(defaultedOptions, { listeners: false })110114 }, [defaultedOptions, observer])111115116+ if (defaultedOptions.experimental_prefetchInRender) {117+ // Do not notify on updates because of changes in the options because118+ // these changes should already be reflected in the optimistic result.119+ observer.setOptions(defaultedOptions, { listeners: false })
TkDodo closed this
Sign in to join this conversation.
View your CI Pipeline Execution ↗ for commit 9797e93a0b6090b5351bf6a07e89deb072125ef6.
nx affected --targets=test:sherif,test:knip,tes...☁️ Nx Cloud last updated this comment at
2025-01-03 14:12:06UTC