'Realtime data collection in firestore referenced by id rather than the whole collection?
I am trying to grab a single doc from firestore from a collection that could have a thousand unrelated data.
My issue is I am getting the whole pack. Can I reference by a particular id?
I don't a user to get other user info in to his front end. I am using a custom hook. Can I use collection(db, col) with doc id?
import React, { useContext, useEffect, useRef, useState, useMemo } from 'react';
// DB
import { db } from '../../firebase';
import { collection, onSnapshot } from 'firebase/firestore';
export const dataCollection = (col: any) => {
const [data, setData] = useState<any>(null);
useEffect(() => {
let colRef = collection(db, col);
const unsub = onSnapshot(colRef, (snapShot) => {
let results: any = [];
snapShot.docs.forEach((doc) => results.push(doc.data()));
console.log(results, 'LOG RESULTS');
setData(results);
return () => unsub;
});
}, [col]);
return { data };
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
