@@ -35,37 +35,34 @@ export const useList = (query?: database.Query | null): ListHook => {
3535 dispatch ( { type : 'remove' , snapshot } ) ;
3636 } ;
3737
38- useEffect (
39- ( ) => {
40- const query : database . Query | null | undefined = ref . current ;
41- if ( ! query ) {
42- dispatch ( { type : 'empty' } ) ;
43- return ;
44- }
45- // This is here to indicate that all the data has been successfully received
46- query . once (
47- 'value' ,
48- ( ) => {
49- dispatch ( { type : 'value' } ) ;
50- } ,
51- ( error : FirebaseError ) => {
52- dispatch ( { type : 'error' , error } ) ;
53- }
54- ) ;
55- query . on ( 'child_added' , onChildAdded ) ;
56- query . on ( 'child_changed' , onChildChanged ) ;
57- query . on ( 'child_moved' , onChildMoved ) ;
58- query . on ( 'child_removed' , onChildRemoved ) ;
38+ const onError = ( error : FirebaseError ) => {
39+ dispatch ( { type : 'error' , error } ) ;
40+ } ;
41+
42+ const onValue = ( ) => {
43+ dispatch ( { type : 'value' } ) ;
44+ } ;
45+
46+ useEffect ( ( ) => {
47+ const query : database . Query | null | undefined = ref . current ;
48+ if ( ! query ) {
49+ dispatch ( { type : 'empty' } ) ;
50+ return ;
51+ }
52+ // This is here to indicate that all the data has been successfully received
53+ query . once ( 'value' , onValue , onError ) ;
54+ query . on ( 'child_added' , onChildAdded , onError ) ;
55+ query . on ( 'child_changed' , onChildChanged , onError ) ;
56+ query . on ( 'child_moved' , onChildMoved , onError ) ;
57+ query . on ( 'child_removed' , onChildRemoved , onError ) ;
5958
60- return ( ) => {
61- query . off ( 'child_added' , onChildAdded ) ;
62- query . off ( 'child_changed' , onChildChanged ) ;
63- query . off ( 'child_moved' , onChildMoved ) ;
64- query . off ( 'child_removed' , onChildRemoved ) ;
65- } ;
66- } ,
67- [ ref . current ]
68- ) ;
59+ return ( ) => {
60+ query . off ( 'child_added' , onChildAdded ) ;
61+ query . off ( 'child_changed' , onChildChanged ) ;
62+ query . off ( 'child_moved' , onChildMoved ) ;
63+ query . off ( 'child_removed' , onChildRemoved ) ;
64+ } ;
65+ } , [ ref . current ] ) ;
6966
7067 return [ state . value . values , state . loading , state . error ] ;
7168} ;
0 commit comments