diff --git a/firestore/useCollection.ts b/firestore/useCollection.ts index eafa2db..1b5427a 100644 --- a/firestore/useCollection.ts +++ b/firestore/useCollection.ts @@ -66,7 +66,7 @@ const useCollectionInternal = ( return; } if (listen) { - const listener = options?.snapshotListenOptions + const unsubscribe = options?.snapshotListenOptions ? onSnapshot( ref.current, options.snapshotListenOptions, @@ -76,11 +76,28 @@ const useCollectionInternal = ( : onSnapshot(ref.current, setValue, setError); return () => { - listener(); + unsubscribe(); }; } else { + let effectActive = true; + const get = getDocsFnFromGetOptions(options?.getOptions); - get(ref.current).then(setValue).catch(setError); + + get(ref.current) + .then((result) => { + if (effectActive) { + setValue(result); + } + }) + .catch((error) => { + if (effectActive) { + setError(error); + } + }); + + return () => { + effectActive = false; + }; } }, [ref.current]); diff --git a/firestore/useDocument.ts b/firestore/useDocument.ts index a4b286f..22946bc 100644 --- a/firestore/useDocument.ts +++ b/firestore/useDocument.ts @@ -65,7 +65,7 @@ const useDocumentInternal = ( return; } if (listen) { - const listener = options?.snapshotListenOptions + const unsubscribe = options?.snapshotListenOptions ? onSnapshot( ref.current, options.snapshotListenOptions, @@ -75,12 +75,28 @@ const useDocumentInternal = ( : onSnapshot(ref.current, setValue, setError); return () => { - listener(); + unsubscribe(); }; } else { + let effectActive = true; + const get = getDocFnFromGetOptions(options?.getOptions); - get(ref.current).then(setValue).catch(setError); + get(ref.current) + .then((doc) => { + if (effectActive) { + setValue(doc); + } + }) + .catch((error) => { + if (effectActive) { + setError(error); + } + }); + + return () => { + effectActive = false; + }; } }, [ref.current]);