import * as React from "react"; import { firestore } from "firebase"; const useFirestoreQuery = ( query: firestore.Query, ): [T[], boolean, Error | null] => { const [isLoading, setIsLoading] = React.useState(true); const [error, setError] = React.useState(null); const [docs, setDocs] = React.useState([]); React.useEffect(() => { const unsubscribe = query.onSnapshot( (querySnapshot: firestore.QuerySnapshot) => { setIsLoading(false); setDocs( querySnapshot.docs.map(doc => ({ _id: doc.id, ...(doc.data() as T), })), ); }, (err: Error) => { setError(err); }, ); return () => unsubscribe(); }, []); return [docs, isLoading, error]; }; export default useFirestoreQuery;