Skip to content

Commit bb0f815

Browse files
authored
Merge pull request #41 from ypresto/memo-data
Add useMemo() for value fetching hooks
2 parents 52883a3 + 9ffdfe6 commit bb0f815

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

database/useList.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { database, FirebaseError } from 'firebase';
2-
import { useEffect } from 'react';
2+
import { useEffect, useMemo } from 'react';
33
import { snapshotToData } from './helpers';
44
import useListReducer from './helpers/useListReducer';
55
import { LoadingHook, useIsEqualRef } from '../util';
@@ -82,14 +82,15 @@ export const useListVals = <T>(
8282
keyField?: string;
8383
}
8484
): ListValsHook<T> => {
85-
const [value, loading, error] = useList(query);
86-
return [
87-
value
88-
? value.map(snapshot =>
89-
snapshotToData(snapshot, options ? options.keyField : undefined)
90-
)
91-
: undefined,
92-
loading,
93-
error,
94-
];
85+
const [snapshots, loading, error] = useList(query);
86+
const values = useMemo(
87+
() =>
88+
snapshots
89+
? snapshots.map(snapshot =>
90+
snapshotToData(snapshot, options ? options.keyField : undefined)
91+
)
92+
: undefined,
93+
[snapshots, options && options.keyField]
94+
);
95+
return [values, loading, error];
9596
};

database/useObject.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { database, FirebaseError } 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

@@ -39,12 +39,13 @@ export const useObjectVal = <T>(
3939
keyField?: string;
4040
}
4141
): ObjectValHook<T> => {
42-
const [value, loading, error] = useObject(query);
43-
return [
44-
value
45-
? snapshotToData(value, options ? options.keyField : undefined)
46-
: undefined,
47-
loading,
48-
error,
49-
];
42+
const [snapshot, loading, error] = useObject(query);
43+
const value = useMemo(
44+
() =>
45+
snapshot
46+
? snapshotToData(snapshot, options ? options.keyField : undefined)
47+
: undefined,
48+
[snapshot, options && options.keyField]
49+
);
50+
return [value, loading, error];
5051
};

firestore/useCollection.ts

Lines changed: 10 additions & 9 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

@@ -54,14 +54,15 @@ export const useCollectionData = <T>(
5454
const snapshotListenOptions = options
5555
? options.snapshotListenOptions
5656
: undefined;
57-
const [value, loading, error] = useCollection(query, {
57+
const [snapshot, loading, error] = useCollection(query, {
5858
snapshotListenOptions,
5959
});
60-
return [
61-
(value
62-
? value.docs.map(doc => snapshotToData(doc, idField))
63-
: undefined) as T[],
64-
loading,
65-
error,
66-
];
60+
const values = useMemo(
61+
() =>
62+
(snapshot
63+
? snapshot.docs.map(doc => snapshotToData(doc, idField))
64+
: undefined) as T[],
65+
[snapshot, idField]
66+
);
67+
return [values, loading, error];
6768
};

firestore/useDocument.ts

Lines changed: 7 additions & 7 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

@@ -54,12 +54,12 @@ export const useDocumentData = <T>(
5454
const snapshotListenOptions = options
5555
? options.snapshotListenOptions
5656
: undefined;
57-
const [value, loading, error] = useDocument(docRef, {
57+
const [snapshot, loading, error] = useDocument(docRef, {
5858
snapshotListenOptions,
5959
});
60-
return [
61-
(value ? snapshotToData(value, idField) : undefined) as T,
62-
loading,
63-
error,
64-
];
60+
const value = useMemo(
61+
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
62+
[snapshot, idField]
63+
);
64+
return [value, loading, error];
6565
};

0 commit comments

Comments
 (0)