Package Rename
react-query → @tanstack/react-query
The package has been renamed and moved to the TanStack organization:Update Imports
Codemod Available
The codemod only changes imports. You must install the new packages manually.
Breaking Changes
Query Keys Must Be Arrays
All query and mutation keys must be arrays:Codemod Available
idle State Removed
The idle state has been replaced with loading + fetchStatus: 'idle':
Why two states?
Why two states?
status reflects the data state (loading/success/error), while fetchStatus reflects the fetch state (idle/fetching/paused). This provides better offline support and more granular control.New useQueries API
undefined is Illegal for Successful Queries
Query functions cannot return undefined:
Network Mode: Queries Need Network by Default
online(default): Requires network connectionofflineFirst: Like v3, always runsalways: Runs even when marked offline
notifyOnChangeProps Default Changed
notifyOnChangePropsExclusions Removed
Now that tracking is default, this option is no longer needed.
cancelRefetch Now Defaults to true
Query Filters Unified
onSuccess Not Called from setQueryData
Persister Plugins Renamed
Promise cancel Method Removed
Use AbortController for query cancellation:
TypeScript 4.1+ Required
TypeScript 4.1 or higher is now required.setLogger Moved to QueryClient
Server-Side: No Manual Garbage Collection
On the server,gcTime now defaults to Infinity instead of 5 minutes:
No Logging in Production
Errors are no longer logged to console in production mode. They still appear in development.Hydration Exports Consolidated
src/react Renamed to src/reactjs
If you imported from react-query/react:
New Features
React 18 Support
Full support for React 18 and concurrent features.Proper Offline Support
NewnetworkMode option provides fine-grained control:
Tracked Queries by Default
Automatic render optimization:Bail Out of setQueryData
Mutation Garbage Collection
Mutations now havegcTime (default 5 minutes):
Custom Contexts for Multiple Providers
Migration Checklist
1
Update Package
2
Run Import Codemod
Update all import statements.
3
Run Key Transformation Codemod
Convert string keys to array keys.
4
Update Query Options
Wrap all options in objects if not already done.
5
Replace idle Checks
Use
isInitialLoading or fetchStatus === 'idle'.6
Update useQueries
Wrap query arrays in
{ queries: [...] }.7
Move onSuccess to useEffect
Replace
onSuccess callbacks with useEffect.8
Update Persister Imports
Use new persister package names.
9
Test Offline Behavior
Verify queries behave correctly when offline.
10
Test Everything
Thoroughly test all query and mutation functionality.
v4 brings significant improvements to offline support, TypeScript types, and render optimization. The migration effort is worth it!