Last active
July 3, 2022 19:11
-
-
Save 3nvi/f9270f666405260f541a9808e3c62053 to your computer and use it in GitHub Desktop.
A simple way to integrate react-query & firebase
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
import React from 'react'; | |
import { useQuery, useQueryClient, UseQueryOptions } from 'react-query'; | |
import realTimeApi from './real-time-api'; | |
function useRealTimeQuery<Data>( | |
firebasePathKey: string, | |
useQueryOptions: UseQueryOptions<Data> = {} | |
) { | |
const queryClient = useQueryClient(); | |
React.useEffect(() => { | |
const unsubscribe = realTimeApi.subscribe<Data>({ | |
path: firebasePathKey, | |
callback: val => { | |
queryClient.setQueryData(firebasePathKey, val); | |
}, | |
}); | |
return () => unsubscribe(); | |
}, [queryClient, firebasePathKey]); | |
return useQuery<Data, Error>( | |
firebasePathKey, | |
() => new Promise<Data>(() => {}), | |
useQueryOptions | |
); | |
} | |
export default useRealTimeQuery; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment