react-code-dataset / react-query /packages /react-query-persist-client /src /PersistQueryClientProvider.tsx
Download react-query/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx
-
curl -L -o PersistQueryClientProvider.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx
1.75 kB
| 'use client' | |
| import * as React from 'react' | |
| import { | |
| persistQueryClientRestore, | |
| persistQueryClientSubscribe, | |
| } from '@tanstack/query-persist-client-core' | |
| import { IsRestoringProvider, QueryClientProvider } from '@tanstack/react-query' | |
| import type { PersistQueryClientOptions } from '@tanstack/query-persist-client-core' | |
| import type { OmitKeyof, QueryClientProviderProps } from '@tanstack/react-query' | |
| export type PersistQueryClientProviderProps = QueryClientProviderProps & { | |
| persistOptions: OmitKeyof<PersistQueryClientOptions, 'queryClient'> | |
| onSuccess?: () => Promise<unknown> | unknown | |
| onError?: () => Promise<unknown> | unknown | |
| } | |
| export const PersistQueryClientProvider = ({ | |
| children, | |
| persistOptions, | |
| onSuccess, | |
| onError, | |
| ...props | |
| }: PersistQueryClientProviderProps): React.JSX.Element => { | |
| const [isRestoring, setIsRestoring] = React.useState(true) | |
| const refs = React.useRef({ persistOptions, onSuccess, onError }) | |
| const didRestore = React.useRef(false) | |
| React.useEffect(() => { | |
| refs.current = { persistOptions, onSuccess, onError } | |
| }) | |
| React.useEffect(() => { | |
| const options = { | |
| ...refs.current.persistOptions, | |
| queryClient: props.client, | |
| } | |
| if (!didRestore.current) { | |
| didRestore.current = true | |
| persistQueryClientRestore(options) | |
| .then(() => refs.current.onSuccess?.()) | |
| .catch(() => refs.current.onError?.()) | |
| .finally(() => { | |
| setIsRestoring(false) | |
| }) | |
| } | |
| return isRestoring ? undefined : persistQueryClientSubscribe(options) | |
| }, [props.client, isRestoring]) | |
| return ( | |
| <QueryClientProvider {...props}> | |
| <IsRestoringProvider value={isRestoring}>{children}</IsRestoringProvider> | |
| </QueryClientProvider> | |
| ) | |
| } | |