File tree Expand file tree Collapse file tree 4 files changed +39
-36
lines changed
Expand file tree Collapse file tree 4 files changed +39
-36
lines changed Original file line number Diff line number Diff line change 11import { database, FirebaseError } from 'firebase';
2- import { useEffect } from 'react';
2+ import { useEffect, useMemo } from 'react';
33import { snapshotToData } from './helpers';
44import useListReducer from './helpers/useListReducer';
55import { 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};
Original file line number Diff line number Diff line change 11import { database, FirebaseError } from 'firebase';
2- import { useEffect } from 'react';
2+ import { useEffect, useMemo } from 'react';
33import { snapshotToData } from './helpers';
44import { 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};
Original file line number Diff line number Diff line change 11import { firestore } from 'firebase';
2- import { useEffect } from 'react';
2+ import { useEffect, useMemo } from 'react';
33import { snapshotToData } from './helpers';
44import { 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};
Original file line number Diff line number Diff line change 11import { firestore } from 'firebase';
2- import { useEffect } from 'react';
2+ import { useEffect, useMemo } from 'react';
33import { snapshotToData } from './helpers';
44import { 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};
You can’t perform that action at this time.
0 commit comments