Skip to content

Commit c55850c

Browse files
committed
Tweak some firestore helpers
1 parent 0f2f827 commit c55850c

File tree

5 files changed

+50
-53
lines changed

5 files changed

+50
-53
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A set of reusable [React Hooks](https://reactjs.org/docs/hooks-intro.html) for [
77

88
This documentation is for v4 of React Firebase Hooks which makes the package compatible with Firebase v9 and drops support for previous versions of Firebase - more details [here](https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v4.0.0).
99

10-
- For v3 documentation (Firebase v8), see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v3.0.0).
10+
- For v3 documentation (Firebase v8), see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v3.0.4).
1111
- For v2 documentation, see [here](https://github.com/CSFrequency/react-firebase-hooks/tree/v2.2.0).
1212

1313
## Installation

firestore/helpers/index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import {
2+
CollectionReference,
23
DocumentData,
4+
DocumentReference,
35
DocumentSnapshot,
6+
Query,
7+
queryEqual,
8+
refEqual,
49
SnapshotOptions,
510
} from 'firebase/firestore';
11+
import { RefHook, useComparatorRef } from '../../util';
612

713
export const snapshotToData = <T = DocumentData>(
814
snapshot: DocumentSnapshot<T>,
@@ -28,3 +34,39 @@ export const snapshotToData = <T = DocumentData>(
2834

2935
return data;
3036
};
37+
38+
const isRefEqual = <
39+
T extends DocumentReference<any> | CollectionReference<any>
40+
>(
41+
v1: T | null | undefined,
42+
v2: T | null | undefined
43+
): boolean => {
44+
const bothNull: boolean = !v1 && !v2;
45+
const equal: boolean = !!v1 && !!v2 && refEqual(v1, v2);
46+
return bothNull || equal;
47+
};
48+
49+
export const useIsFirestoreRefEqual = <
50+
T extends DocumentReference<any> | CollectionReference<any>
51+
>(
52+
value: T | null | undefined,
53+
onChange?: () => void
54+
): RefHook<T | null | undefined> => {
55+
return useComparatorRef(value, isRefEqual, onChange);
56+
};
57+
58+
const isQueryEqual = <T extends Query<any>>(
59+
v1: T | null | undefined,
60+
v2: T | null | undefined
61+
): boolean => {
62+
const bothNull: boolean = !v1 && !v2;
63+
const equal: boolean = !!v1 && !!v2 && queryEqual(v1, v2);
64+
return bothNull || equal;
65+
};
66+
67+
export const useIsFirestoreQueryEqual = <T extends Query<any>>(
68+
value: T | null | undefined,
69+
onChange?: () => void
70+
): RefHook<T | null | undefined> => {
71+
return useComparatorRef(value, isQueryEqual, onChange);
72+
};

firestore/useCollection.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from 'firebase/firestore';
1111
import { useEffect, useMemo } from 'react';
1212
import { useLoadingValue } from '../util';
13-
import { snapshotToData } from './helpers';
13+
import { snapshotToData, useIsFirestoreQueryEqual } from './helpers';
1414
import {
1515
CollectionDataHook,
1616
CollectionHook,
@@ -21,7 +21,6 @@ import {
2121
OnceOptions,
2222
Options,
2323
} from './types';
24-
import { useIsEqualFirestoreQuery } from './util';
2524

2625
export const useCollection = <T = DocumentData>(
2726
query?: Query<T> | null,
@@ -68,7 +67,7 @@ const useCollectionInternal = <T = DocumentData>(
6867
QuerySnapshot<T>,
6968
FirestoreError
7069
>();
71-
const ref = useIsEqualFirestoreQuery<Query<T>>(query, reset);
70+
const ref = useIsFirestoreQueryEqual<Query<T>>(query, reset);
7271

7372
useEffect(() => {
7473
if (!ref.current) {
@@ -90,7 +89,9 @@ const useCollectionInternal = <T = DocumentData>(
9089
listener();
9190
};
9291
} else {
93-
const get = getDocsFnFromGetOptions(options ? options.getOptions : undefined);
92+
const get = getDocsFnFromGetOptions(
93+
options ? options.getOptions : undefined
94+
);
9495
get(ref.current).then(setValue).catch(setError);
9596
}
9697
}, [ref.current]);

firestore/useDocument.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from 'firebase/firestore';
1111
import { useEffect, useMemo } from 'react';
1212
import { useLoadingValue } from '../util';
13-
import { snapshotToData } from './helpers';
13+
import { snapshotToData, useIsFirestoreRefEqual } from './helpers';
1414
import {
1515
Data,
1616
DataOptions,
@@ -21,8 +21,6 @@ import {
2121
OnceOptions,
2222
Options,
2323
} from './types';
24-
import { useIsEqualFirestoreRef } from './util';
25-
2624
export const useDocument = <T = DocumentData>(
2725
docRef?: DocumentReference<T> | null,
2826
options?: Options
@@ -68,7 +66,7 @@ const useDocumentInternal = <T = DocumentData>(
6866
DocumentSnapshot<T>,
6967
FirestoreError
7068
>();
71-
const ref = useIsEqualFirestoreRef<DocumentReference<T>>(docRef, reset);
69+
const ref = useIsFirestoreRefEqual<DocumentReference<T>>(docRef, reset);
7270

7371
useEffect(() => {
7472
if (!ref.current) {

firestore/util.ts

-44
This file was deleted.

0 commit comments

Comments
 (0)