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' ;
61import {
72 DataSnapshot ,
8- Query ,
3+ off ,
94 onChildAdded as firebaseOnChildAdded ,
105 onChildChanged as firebaseOnChildChanged ,
116 onChildMoved as firebaseOnChildMoved ,
127 onChildRemoved as firebaseOnChildRemoved ,
138 onValue as firebaseOnValue ,
14- off ,
9+ Query ,
1510} 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' ;
1616
1717export const useList = ( query ?: Query | null ) : ListHook => {
1818 const [ state , dispatch ] = useListReducer ( ) ;
@@ -118,8 +118,7 @@ export const useList = (query?: Query | null): ListHook => {
118118 } ;
119119 } , [ dispatch , ref ] ) ;
120120
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 ] ;
123122} ;
124123
125124export const useListKeys = ( query ?: Query | null ) : ListKeysHook => {
@@ -131,9 +130,8 @@ export const useListKeys = (query?: Query | null): ListKeysHook => {
131130 : undefined ,
132131 [ snapshots ]
133132 ) ;
134- const resArray : ListKeysHook = [ values , loading , error ] ;
135133
136- return useMemo ( ( ) => resArray , resArray ) ;
134+ return [ values , loading , error ] ;
137135} ;
138136
139137export const useListVals = <
@@ -144,9 +142,15 @@ export const useListVals = <
144142 query ?: Query | null ,
145143 options ?: ValOptions < T >
146144) : 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+
150154 const [ snapshots , loading , error ] = useList ( query ) ;
151155 const values = useMemo (
152156 ( ) =>
@@ -158,10 +162,5 @@ export const useListVals = <
158162 [ snapshots , keyField , refField , transform ]
159163 ) ;
160164
161- const resArray : ListValsHook < T , KeyField , RefField > = [
162- values ,
163- loading ,
164- error ,
165- ] ;
166- return useMemo ( ( ) => resArray , resArray ) ;
165+ return [ values , loading , error ] ;
167166} ;
0 commit comments