Skip to content

Commit 35f8fd7

Browse files
committed
[database] Add useListVal hook
1 parent aad65c7 commit 35f8fd7

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Parameters:
3737
- `auth`: `firebase.auth.Auth`
3838

3939
Returns:
40-
`AuthState` containing:
40+
`AuthStateHook` containing:
4141
- `initialising`: If the listener is still waiting for the user to be loaded
4242
- `user`: The `firebase.User`, or `null`, if no user is logged in
4343

@@ -90,7 +90,7 @@ Parameters:
9090
- `query`: `firebase.firestore.Query`
9191

9292
Returns:
93-
`CollectionValue` containing
93+
`CollectionHook` containing
9494
- `error`: An optional `firebase.FirebaseError` returned by Firebase
9595
- `loading`: A `boolean` to indicate if the listener is still being loaded
9696
- `value`: A `firebase.firestore.QuerySnapshot`
@@ -126,7 +126,7 @@ Parameters:
126126
- `docRef`: `firebase.firestore.DocumentReference`
127127

128128
Returns:
129-
`DocumentValue` containing
129+
`DocumentHook` containing
130130
- `error`: An optional `firebase.FirebaseError` returned by Firebase
131131
- `loading`: A `boolean` to indicate if the listener is still being loaded
132132
- `value`: A `firebase.firestore.DocumentSnapshot`
@@ -168,7 +168,7 @@ Parameters:
168168
- `ref`: `firebase.database.Reference`
169169

170170
Returns:
171-
`ListValue` containing
171+
`ListHook` containing
172172
- `error`: An optional error object returned by Firebase
173173
- `loading`: A `boolean` to indicate if the listener is still being loaded
174174
- `value`: A list of `firebase.database.DataSnapshot`
@@ -199,13 +199,30 @@ const DatabaseList = () => {
199199
};
200200
```
201201

202+
#### `useListVal<T>(ref, keyField)`
203+
204+
As above, but this hook returns a typed list of the `DataSnapshot.val()` values, rather than the the
205+
`DataSnapshot`s themselves.
206+
207+
Parameters:
208+
- `ref`: `firebase.database.Reference`
209+
- `keyField`: (Optional) Name of field that should be populated with the `DataSnapshot.key` property
210+
211+
Returns:
212+
`ListValHook` containing
213+
- `error`: An optional error object returned by Firebase
214+
- `loading`: A `boolean` to indicate if the listener is still being loaded
215+
- `value`: A list of the contents of `firebase.database.DataSnapshot.val()` and optional key field
216+
217+
```
218+
202219
#### `useObject(ref)`
203220
204221
Parameters:
205222
- `ref`: `firebase.database.Reference`
206223
207224
Returns:
208-
`ObjectValue` containing
225+
`ObjectHook` containing
209226
- `error`: An optional error object returned by Firebase
210227
- `loading`: A `boolean` to indicate if the listener is still being loaded
211228
- `value`: A `firebase.database.DataSnapshot`
@@ -239,10 +256,10 @@ Parameters:
239256
- `ref`: `firebase.database.Reference`
240257

241258
Returns:
242-
`ObjectValue` containing
259+
`ObjectValHook` containing
243260
- `error`: An optional error object returned by Firebase
244261
- `loading`: A `boolean` to indicate if the listener is still being loaded
245-
- `value`: A `firebase.database.DataSnapshot`
262+
- `value`: The contents of `firebase.database.DataSnapshot.val()`
246263

247264
```
248265

database/index.js.flow

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export type ListHook = {
66
loading: boolean;
77
value: DataSnapshot[];
88
};
9+
export type ListValHook<T> = {
10+
error?: Object;
11+
loading: boolean;
12+
value: T[];
13+
};
914
export type ObjectHook = {
1015
error?: Object;
1116
loading: boolean;
@@ -18,5 +23,6 @@ export type ObjectValHook<T> = {
1823
};
1924

2025
declare export function useList(query: Query): ListHook;
26+
declare export function useListVal(query: Query, keyField?: string): ListHook;
2127
declare export function useObject(query: Query): ObjectHook;
2228
declare export function useObjectVal<T>(query: Query): ObjectValHook<T>;

database/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as useList, ListHook } from './useList';
2+
export { default as useListVal, ListValHook } from './useListVal';
23
export { default as useObject, ObjectHook } from './useObject';
34
export { default as useObjectVal, ObjectValHook } from './useObjectVal';

database/useListVal.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { database } from 'firebase';
2+
import useList from './useList';
3+
4+
export type ListValHook<T> = {
5+
error?: Object;
6+
loading: boolean;
7+
value: T[];
8+
};
9+
10+
export const useListVal = <T>(
11+
query: database.Query,
12+
keyField?: string
13+
): ListValHook<T> => {
14+
const { error, loading, value } = useList(query);
15+
return {
16+
error,
17+
loading,
18+
value: value.map(snapshot => {
19+
return keyField
20+
? {
21+
...snapshot.val(),
22+
[keyField]: snapshot.key,
23+
}
24+
: snapshot.val();
25+
}),
26+
};
27+
};

0 commit comments

Comments
 (0)