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 1
1
import { database , FirebaseError } from 'firebase' ;
2
- import { useEffect } from 'react' ;
2
+ import { useEffect , useMemo } from 'react' ;
3
3
import { snapshotToData } from './helpers' ;
4
4
import useListReducer from './helpers/useListReducer' ;
5
5
import { LoadingHook , useIsEqualRef } from '../util' ;
@@ -82,14 +82,15 @@ export const useListVals = <T>(
82
82
keyField ?: string ;
83
83
}
84
84
) : 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 ] ;
95
96
} ;
Original file line number Diff line number Diff line change 1
1
import { database , FirebaseError } from 'firebase' ;
2
- import { useEffect } from 'react' ;
2
+ import { useEffect , useMemo } from 'react' ;
3
3
import { snapshotToData } from './helpers' ;
4
4
import { LoadingHook , useIsEqualRef , useLoadingValue } from '../util' ;
5
5
@@ -39,12 +39,13 @@ export const useObjectVal = <T>(
39
39
keyField ?: string ;
40
40
}
41
41
) : 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 ] ;
50
51
} ;
Original file line number Diff line number Diff line change 1
1
import { firestore } from 'firebase' ;
2
- import { useEffect } from 'react' ;
2
+ import { useEffect , useMemo } from 'react' ;
3
3
import { snapshotToData } from './helpers' ;
4
4
import { LoadingHook , useIsEqualRef , useLoadingValue } from '../util' ;
5
5
@@ -54,14 +54,15 @@ export const useCollectionData = <T>(
54
54
const snapshotListenOptions = options
55
55
? options . snapshotListenOptions
56
56
: undefined ;
57
- const [ value , loading , error ] = useCollection ( query , {
57
+ const [ snapshot , loading , error ] = useCollection ( query , {
58
58
snapshotListenOptions,
59
59
} ) ;
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 ] ;
67
68
} ;
Original file line number Diff line number Diff line change 1
1
import { firestore } from 'firebase' ;
2
- import { useEffect } from 'react' ;
2
+ import { useEffect , useMemo } from 'react' ;
3
3
import { snapshotToData } from './helpers' ;
4
4
import { LoadingHook , useIsEqualRef , useLoadingValue } from '../util' ;
5
5
@@ -54,12 +54,12 @@ export const useDocumentData = <T>(
54
54
const snapshotListenOptions = options
55
55
? options . snapshotListenOptions
56
56
: undefined ;
57
- const [ value , loading , error ] = useDocument ( docRef , {
57
+ const [ snapshot , loading , error ] = useDocument ( docRef , {
58
58
snapshotListenOptions,
59
59
} ) ;
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 ] ;
65
65
} ;
You can’t perform that action at this time.
0 commit comments