Skip to content

Commit 6826054

Browse files
committed
[database] Tidy up unnecessary memoization
1 parent fda7ab4 commit 6826054

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

database/useList.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
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';
61
import {
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

1717
export 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

125124
export 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

139137
export 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
};

database/useObject.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { DataSnapshot, off, onValue, Query } from 'firebase/database';
12
import { useEffect, useMemo } from 'react';
3+
import { useIsEqualRef, useLoadingValue } from '../util';
24
import { snapshotToData, ValOptions } from './helpers';
35
import { ObjectHook, ObjectValHook, Val } from './types';
4-
import { useIsEqualRef, useLoadingValue } from '../util';
5-
import { DataSnapshot, off, onValue, Query } from 'firebase/database';
66

77
export const useObject = (query?: Query | null): ObjectHook => {
88
const { error, loading, reset, setError, setValue, value } = useLoadingValue<
@@ -25,8 +25,7 @@ export const useObject = (query?: Query | null): ObjectHook => {
2525
};
2626
}, [ref.current]);
2727

28-
const resArray: ObjectHook = [value, loading, error];
29-
return useMemo(() => resArray, resArray);
28+
return [value, loading, error];
3029
};
3130

3231
export const useObjectVal = <
@@ -37,9 +36,15 @@ export const useObjectVal = <
3736
query?: Query | null,
3837
options?: ValOptions<T>
3938
): ObjectValHook<T, KeyField, RefField> => {
40-
const keyField = options ? options.keyField : undefined;
41-
const refField = options ? options.refField : undefined;
42-
const transform = options ? options.transform : undefined;
39+
// Breaking this out in both `useList` and `useObject` rather than
40+
// within the `snapshotToData` prevents the developer needing to ensure
41+
// that `options` is memoized. If `options` was passed directly then it
42+
// would cause the values to be recalculated every time the whole
43+
// `options object changed
44+
const keyField = options?.keyField || undefined;
45+
const refField = options?.refField || undefined;
46+
const transform = options?.transform || undefined;
47+
4348
const [snapshot, loading, error] = useObject(query);
4449
const value = useMemo(
4550
() =>
@@ -49,10 +54,5 @@ export const useObjectVal = <
4954
[snapshot, keyField, refField, transform]
5055
);
5156

52-
const resArray: ObjectValHook<T, KeyField, RefField> = [
53-
value,
54-
loading,
55-
error,
56-
];
57-
return useMemo(() => resArray, resArray);
57+
return [value, loading, error];
5858
};

0 commit comments

Comments
 (0)