-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for default options when hydrating
- Loading branch information
Showing
8 changed files
with
76 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
import React from 'react' | ||
|
||
import { useQueryClient } from '../react' | ||
import { hydrate } from './hydration' | ||
import { hydrate, HydrateOptions } from './hydration' | ||
|
||
export function useHydrate(queries: unknown) { | ||
export function useHydrate(state: unknown, options?: HydrateOptions) { | ||
const client = useQueryClient() | ||
const cache = client.getCache() | ||
|
||
const optionsRef = React.useRef(options) | ||
optionsRef.current = options | ||
|
||
// Running hydrate again with the same queries is safe, | ||
// it wont overwrite or initialize existing queries, | ||
// relying on useMemo here is only a performance optimization | ||
React.useMemo(() => { | ||
if (queries) { | ||
hydrate(cache, queries) | ||
if (state) { | ||
hydrate(cache, state, optionsRef.current) | ||
} | ||
}, [cache, queries]) | ||
}, [cache, state]) | ||
} | ||
|
||
export interface HydrateProps { | ||
state?: unknown | ||
options?: HydrateOptions | ||
} | ||
|
||
export const Hydrate: React.FC<HydrateProps> = ({ state, children }) => { | ||
useHydrate(state) | ||
export const Hydrate: React.FC<HydrateProps> = ({ | ||
children, | ||
options, | ||
state, | ||
}) => { | ||
useHydrate(state, options) | ||
return children as React.ReactElement<any> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters