-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
174 additions
and
143 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
DehydratedState, | ||
QueryClientProvider, | ||
useQueryClient, | ||
} from '@tanstack/react-query' | ||
import { ReactNode, useEffect, useMemo } from 'react' | ||
import { RecoilRoot, useSetRecoilState } from 'recoil' | ||
|
||
import { makeReactQueryClient, queryClientAtom } from '@dao-dao/state' | ||
|
||
export type StateProviderProps = { | ||
/** | ||
* Children to render. | ||
*/ | ||
children: ReactNode | ||
/** | ||
* Optional dehyrated state from a react-query client instance on the server | ||
* to initialize data. | ||
*/ | ||
dehyratedState?: DehydratedState | ||
} | ||
|
||
/** | ||
* A provider that wraps an app with the state providers, like React Query and | ||
* Recoil. | ||
*/ | ||
export const StateProvider = ({ | ||
children, | ||
dehyratedState, | ||
}: StateProviderProps) => { | ||
const client = useMemo( | ||
() => makeReactQueryClient(dehyratedState), | ||
[dehyratedState] | ||
) | ||
|
||
return ( | ||
<QueryClientProvider client={client}> | ||
<RecoilRoot | ||
initializeState={ | ||
// Give query client to Recoil so selectors can access queries. | ||
({ set }) => set(queryClientAtom, client) | ||
} | ||
> | ||
<InnerStateProvider>{children}</InnerStateProvider> | ||
</RecoilRoot> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
const InnerStateProvider = ({ children }: { children: ReactNode }) => { | ||
const queryClient = useQueryClient() | ||
const setQueryClient = useSetRecoilState(queryClientAtom) | ||
// Update Recoil atom when the query client changes. | ||
useEffect(() => { | ||
setQueryClient(queryClient) | ||
}, [queryClient, setQueryClient]) | ||
|
||
return <>{children} </> | ||
} |
Oops, something went wrong.