import * as React from 'react'
import {
MutationCache,
QueryClient,
onlineManager,
useIsRestoring,
useQuery,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { Toaster, toast } from 'react-hot-toast'
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
import {
Link,
Outlet,
ReactLocation,
Router,
useMatch,
} from '@tanstack/react-location'
import * as api from './api'
import { movieKeys, useMovie } from './movies'
const persister = createAsyncStoragePersister({
storage: window.localStorage,
})
const location = new ReactLocation()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
gcTime: 1000 * 60 * 60 * 24, // 24 hours
staleTime: 2000,
retry: 0,
},
},
// configure global cache callbacks to show toast notifications
mutationCache: new MutationCache({
onSuccess: (data) => {
toast.success(data.message)
},
onError: (error) => {
toast.error(error.message)
},
}),
})
// we need a default mutation function so that paused mutations can resume after a page reload
queryClient.setMutationDefaults(movieKeys.all(), {
mutationFn: async ({ id, comment }) => {
// to avoid clashes with our optimistic update when an offline mutation continues
await queryClient.cancelQueries({ queryKey: movieKeys.detail(id) })
return api.updateMovie(id, comment)
},
})
export default function App() {
return (
Try to mock offline behaviour with the button in the devtools. You can navigate around as long as there is already data in the cache. You'll get a refetch as soon as you go online again.