Skip to content

Commit cf815de

Browse files
committed
Memoize useListKeys', useCollectionOnce's, useDocumentDataOnce's value
This hook should only return a new array of keys if the snapshots change. Previously, a new array was being returned every render cycle.
1 parent 2bdb336 commit cf815de

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

database/useList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ export const useList = (query?: database.Query | null): ListHook => {
7272
};
7373

7474
export const useListKeys = (query?: database.Query | null): ListKeysHook => {
75-
const [value, loading, error] = useList(query);
75+
const [snapshots, loading, error] = useList(query);
76+
const values = useMemo(
77+
() => (snapshots ? snapshots.map(snapshot => snapshot.key as string) : undefined),
78+
[snapshots]
79+
);
7680
const resArray: ListKeysHook = [
77-
value ? value.map(snapshot => snapshot.key as string) : undefined,
81+
values,
7882
loading,
7983
error,
8084
];

firestore/useCollection.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ export const useCollectionData = <T>(
5858
const snapshotListenOptions = options
5959
? options.snapshotListenOptions
6060
: undefined;
61-
const [snapshot, loading, error] = useCollection(query, {
61+
const [snapshots, loading, error] = useCollection(query, {
6262
snapshotListenOptions,
6363
});
6464
const values = useMemo(
65-
() =>
66-
(snapshot
67-
? snapshot.docs.map(doc => snapshotToData(doc, idField))
68-
: undefined) as T[],
69-
[snapshot, idField]
65+
() => (snapshots
66+
? snapshots.docs.map(doc => snapshotToData(doc, idField))
67+
: undefined) as T[],
68+
[snapshots, idField]
7069
);
7170

7271
const resArray: CollectionDataHook<T> = [values, loading, error]

firestore/useCollectionOnce.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ export const useCollectionDataOnce = <T>(
4848
): CollectionDataOnceHook<T> => {
4949
const idField = options ? options.idField : undefined;
5050
const getOptions = options ? options.getOptions : undefined;
51-
const [value, loading, error] = useCollectionOnce(query, { getOptions });
52-
const resArray: CollectionDataOnceHook<T> = [
53-
(value
54-
? value.docs.map(doc => snapshotToData(doc, idField))
51+
const [snapshots, loading, error] = useCollectionOnce(query, { getOptions });
52+
const values = useMemo(
53+
() => (snapshots
54+
? snapshots.docs.map(doc => snapshotToData(doc, idField))
5555
: undefined) as T[],
56+
[snapshots, idField]
57+
);
58+
const resArray: CollectionDataOnceHook<T> = [
59+
values,
5660
loading,
5761
error,
5862
];

firestore/useDocumentOnce.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { firestore } from 'firebase';
2-
import { useEffect } from 'react';
2+
import { useEffect, useMemo } from 'react';
33
import { snapshotToData } from './helpers';
44
import { LoadingHook, useIsEqualRef, useLoadingValue } from '../util';
55

@@ -32,7 +32,11 @@ export const useDocumentOnce = (
3232
[ref.current]
3333
);
3434

35-
return [value, loading, error];
35+
const resArray: DocumentOnceHook = [value, loading, error]
36+
return useMemo(
37+
() => resArray,
38+
resArray,
39+
);
3640
};
3741

3842
export const useDocumentDataOnce = <T>(
@@ -44,10 +48,15 @@ export const useDocumentDataOnce = <T>(
4448
): DocumentDataOnceHook<T> => {
4549
const idField = options ? options.idField : undefined;
4650
const getOptions = options ? options.getOptions : undefined;
47-
const [value, loading, error] = useDocumentOnce(docRef, { getOptions });
48-
return [
49-
(value ? snapshotToData(value, idField) : undefined) as T,
50-
loading,
51-
error,
52-
];
51+
const [snapshot, loading, error] = useDocumentOnce(docRef, { getOptions });
52+
const value = useMemo(
53+
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
54+
[snapshot, idField]
55+
);
56+
57+
const resArray: DocumentDataOnceHook<T> = [value, loading, error]
58+
return useMemo(
59+
() => resArray,
60+
resArray,
61+
);
5362
};

0 commit comments

Comments
 (0)