erayerdin / firereact

React hooks, components and utilities for Firebase
https://firereact.erayerdin.com
MIT License
8 stars 0 forks source link

Infinite Renders on `useCollection` #95

Open erayerdin opened 1 month ago

erayerdin commented 1 month ago

Ref: https://stackoverflow.com/questions/57227400/is-infinite-looping-on-firebase-onsnapshot-function-normal

erayerdin commented 1 month ago

This below seems like it solves the issue, experimenting with it:

const colRef = useMemo(() => collection(firestore, 'signupRequests'), [firestore]);
const colQuery = useMemo(() => query(colRef, limit(25), orderBy('requestDate', 'desc')), [colRef]);
const [snapshot, setSnapshot] = useState<QuerySnapshot<DocumentData, DocumentData> | undefined>();
erayerdin commented 3 weeks ago

So, wrapping Firestore, CollectionReference and CollectionQuery instances with useMemo gets rid of the rerender.