1
- import { useEffect , useMemo } from 'react' ;
2
- import { snapshotToData , ValOptions } from './helpers' ;
3
- import useListReducer from './helpers/useListReducer' ;
4
- import { ListHook , ListKeysHook , ListValsHook , Val } from './types' ;
5
- import { useIsEqualRef } from '../util' ;
6
1
import {
7
2
DataSnapshot ,
8
- Query ,
3
+ off ,
9
4
onChildAdded as firebaseOnChildAdded ,
10
5
onChildChanged as firebaseOnChildChanged ,
11
6
onChildMoved as firebaseOnChildMoved ,
12
7
onChildRemoved as firebaseOnChildRemoved ,
13
8
onValue as firebaseOnValue ,
14
- off ,
9
+ Query ,
15
10
} from 'firebase/database' ;
11
+ import { useEffect , useMemo } from 'react' ;
12
+ import { useIsEqualRef } from '../util' ;
13
+ import { snapshotToData , ValOptions } from './helpers' ;
14
+ import useListReducer from './helpers/useListReducer' ;
15
+ import { ListHook , ListKeysHook , ListValsHook , Val } from './types' ;
16
16
17
17
export const useList = ( query ?: Query | null ) : ListHook => {
18
18
const [ state , dispatch ] = useListReducer ( ) ;
@@ -118,8 +118,7 @@ export const useList = (query?: Query | null): ListHook => {
118
118
} ;
119
119
} , [ dispatch , ref ] ) ;
120
120
121
- const resArray : ListHook = [ state . value . values , state . loading , state . error ] ;
122
- return useMemo ( ( ) => resArray , resArray ) ;
121
+ return [ state . value . values , state . loading , state . error ] ;
123
122
} ;
124
123
125
124
export const useListKeys = ( query ?: Query | null ) : ListKeysHook => {
@@ -131,9 +130,8 @@ export const useListKeys = (query?: Query | null): ListKeysHook => {
131
130
: undefined ,
132
131
[ snapshots ]
133
132
) ;
134
- const resArray : ListKeysHook = [ values , loading , error ] ;
135
133
136
- return useMemo ( ( ) => resArray , resArray ) ;
134
+ return [ values , loading , error ] ;
137
135
} ;
138
136
139
137
export const useListVals = <
@@ -144,9 +142,15 @@ export const useListVals = <
144
142
query ?: Query | null ,
145
143
options ?: ValOptions < T >
146
144
) : ListValsHook < T , KeyField , RefField > => {
147
- const keyField = options ? options . keyField : undefined ;
148
- const refField = options ? options . refField : undefined ;
149
- const transform = options ? options . transform : undefined ;
145
+ // Breaking this out in both `useList` and `useObject` rather than
146
+ // within the `snapshotToData` prevents the developer needing to ensure
147
+ // that `options` is memoized. If `options` was passed directly then it
148
+ // would cause the values to be recalculated every time the whole
149
+ // `options object changed
150
+ const keyField = options ?. keyField || undefined ;
151
+ const refField = options ?. refField || undefined ;
152
+ const transform = options ?. transform || undefined ;
153
+
150
154
const [ snapshots , loading , error ] = useList ( query ) ;
151
155
const values = useMemo (
152
156
( ) =>
@@ -158,10 +162,5 @@ export const useListVals = <
158
162
[ snapshots , keyField , refField , transform ]
159
163
) ;
160
164
161
- const resArray : ListValsHook < T , KeyField , RefField > = [
162
- values ,
163
- loading ,
164
- error ,
165
- ] ;
166
- return useMemo ( ( ) => resArray , resArray ) ;
165
+ return [ values , loading , error ] ;
167
166
} ;
0 commit comments