Skip to content

Instantly share code, notes, and snippets.

@3nvi
Last active July 3, 2022 19:11
Show Gist options
  • Save 3nvi/f9270f666405260f541a9808e3c62053 to your computer and use it in GitHub Desktop.
Save 3nvi/f9270f666405260f541a9808e3c62053 to your computer and use it in GitHub Desktop.
A simple way to integrate react-query & firebase
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