Skip to content

Commit 49abf45

Browse files
committed
Fix an issue with the hook not updating if the query ref changes
1 parent 34ca031 commit 49abf45

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

database/useList.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
1515
const [state, dispatch] = useListReducer();
1616

1717
const queryRef = useIsEqualRef(query, () => dispatch({ type: 'reset' }));
18+
const ref: firebase.database.Query | null | undefined = queryRef.current;
1819

1920
useEffect(() => {
20-
const ref: firebase.database.Query | null | undefined = queryRef.current;
2121
if (!ref) {
2222
dispatch({ type: 'empty' });
2323
return;
@@ -30,7 +30,9 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
3030
dispatch({ type: 'add', previousKey, snapshot });
3131
};
3232

33-
const onChildChanged = (snapshot: firebase.database.DataSnapshot | null) => {
33+
const onChildChanged = (
34+
snapshot: firebase.database.DataSnapshot | null
35+
) => {
3436
dispatch({ type: 'change', snapshot });
3537
};
3638

@@ -41,7 +43,9 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
4143
dispatch({ type: 'move', previousKey, snapshot });
4244
};
4345

44-
const onChildRemoved = (snapshot: firebase.database.DataSnapshot | null) => {
46+
const onChildRemoved = (
47+
snapshot: firebase.database.DataSnapshot | null
48+
) => {
4549
dispatch({ type: 'remove', snapshot });
4650
};
4751

@@ -60,7 +64,7 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
6064

6165
const onChildAddedWithoutInitialLoad = (
6266
addedChild: firebase.database.DataSnapshot,
63-
previousKey?: string
67+
previousKey?: string | null
6468
) => {
6569
// process the first batch of children all at once
6670
if (childrenToProcess > 0) {
@@ -103,7 +107,7 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
103107
ref.off('child_moved', childMovedHandler);
104108
ref.off('child_removed', childRemovedHandler);
105109
};
106-
}, [dispatch, queryRef]);
110+
}, [dispatch, ref]);
107111

108112
const resArray: ListHook = [state.value.values, state.loading, state.error];
109113
return useMemo(() => resArray, resArray);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"typescript": "2.8.1"
6464
},
6565
"peerDependencies": {
66-
"react": "^16.8.0"
66+
"react": ">= 16.8.0"
6767
},
6868
"typings": "index.d.ts"
6969
}

0 commit comments

Comments
 (0)