77 getDocFromCache ,
88 getDocFromServer ,
99 onSnapshot ,
10+ SnapshotOptions ,
1011} from 'firebase/firestore' ;
1112import { useEffect , useMemo } from 'react' ;
1213import { useLoadingValue } from '../util' ;
@@ -120,13 +121,12 @@ export const useDocumentData = <T = DocumentData>(
120121 docRef ?: DocumentReference < T > | null ,
121122 options ?: DataOptions < T > & InitialValueOptions < T >
122123) : DocumentDataHook < T > => {
123- const snapshotOptions = options ?. snapshotOptions ;
124124 const [ snapshot , loading , error ] = useDocument < T > ( docRef , options ) ;
125125
126- const initialValue = options ?. initialValue ;
127- const value = useMemo (
128- ( ) => ( snapshot ?. data ( snapshotOptions ) ?? initialValue ) as T | undefined ,
129- [ snapshot , snapshotOptions , initialValue ]
126+ const value = getValueFromSnapshot (
127+ snapshot ,
128+ options ?. snapshotOptions ,
129+ options ?. initialValue
130130 ) ;
131131
132132 const resArray : DocumentDataHook < T > = [ value , loading , error , snapshot ] ;
@@ -137,16 +137,15 @@ export const useDocumentDataOnce = <T = DocumentData>(
137137 docRef ?: DocumentReference < T > | null ,
138138 options ?: OnceDataOptions < T > & InitialValueOptions < T >
139139) : DocumentDataOnceHook < T > => {
140- const snapshotOptions = options ?. snapshotOptions ;
141140 const [ snapshot , loading , error , loadData ] = useDocumentOnce < T > (
142141 docRef ,
143142 options
144143 ) ;
145144
146- const initialValue = options ?. initialValue ;
147- const value = useMemo (
148- ( ) => ( snapshot ?. data ( snapshotOptions ) ?? initialValue ) as T | undefined ,
149- [ snapshot , snapshotOptions , initialValue ]
145+ const value = getValueFromSnapshot (
146+ snapshot ,
147+ options ?. snapshotOptions ,
148+ options ?. initialValue
150149 ) ;
151150
152151 const resArray : DocumentDataOnceHook < T > = [
@@ -172,3 +171,14 @@ const getDocFnFromGetOptions = (
172171 return getDocFromServer ;
173172 }
174173} ;
174+
175+ const getValueFromSnapshot = < T > (
176+ snapshot : DocumentSnapshot < T > | undefined ,
177+ options ?: SnapshotOptions ,
178+ initialValue ?: T
179+ ) : T | undefined => {
180+ return useMemo (
181+ ( ) => ( snapshot ?. data ( options ) ?? initialValue ) as T | undefined ,
182+ [ snapshot , options , initialValue ]
183+ ) ;
184+ } ;
0 commit comments