Skip to content

Commit e983fe5

Browse files
committed
[database] Add useListKeys hook
1 parent 1a62f34 commit e983fe5

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,36 @@ const DatabaseList = () => {
199199
};
200200
```
201201

202-
#### `useListVal<T>(ref, keyField)`
202+
#### `useListKeys<T>(ref)`
203203

204-
As above, but this hook returns a typed list of the `DataSnapshot.val()` values, rather than the the
204+
As above, but this hook returns a list of the `DataSnapshot.key` values, rather than the the
205+
`DataSnapshot`s themselves.
206+
207+
Parameters:
208+
- `ref`: `firebase.database.Reference`
209+
210+
Returns:
211+
`ListKeysHook` containing
212+
- `error`: An optional error object returned by Firebase
213+
- `loading`: A `boolean` to indicate if the listener is still being loaded
214+
- `value`: A list of `firebase.database.DataSnapshot.key` values
215+
216+
```
217+
218+
#### `useListVals<T>(ref, keyField)`
219+
220+
Similar to `useList`, but this hook returns a typed list of the `DataSnapshot.val()` values, rather than the the
205221
`DataSnapshot`s themselves.
206222
207223
Parameters:
208224
- `ref`: `firebase.database.Reference`
209225
- `keyField`: (Optional) Name of field that should be populated with the `DataSnapshot.key` property
210226
211227
Returns:
212-
`ListValHook` containing
228+
`ListValsHook` containing
213229
- `error`: An optional error object returned by Firebase
214230
- `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
231+
- `value`: A list of `firebase.database.DataSnapshot.val()` values, combined with the optional key field
216232
217233
```
218234

database/index.js.flow

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ export type ListHook = {
66
loading: boolean;
77
value: DataSnapshot[];
88
};
9-
export type ListValHook<T> = {
9+
export type ListKeysHook<T> = {
10+
error?: Object;
11+
loading: boolean;
12+
value: string[];
13+
};
14+
export type ListValsHook<T> = {
1015
error?: Object;
1116
loading: boolean;
1217
value: T[];
@@ -23,6 +28,7 @@ export type ObjectValHook<T> = {
2328
};
2429

2530
declare export function useList(query: Query): ListHook;
26-
declare export function useListVal<T>(query: Query, keyField?: string): ListValHook<T>;
31+
declare export function useListKeys<T>(query: Query): ListKeysHook<T>;
32+
declare export function useListVals<T>(query: Query, keyField?: string): ListValsHook<T>;
2733
declare export function useObject(query: Query): ObjectHook;
2834
declare export function useObjectVal<T>(query: Query): ObjectValHook<T>;

database/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { default as useList, ListHook } from './useList';
2-
export { default as useListVal, ListValHook } from './useListVal';
2+
export { default as useListVals, ListValsHook } from './useListVals';
33
export { default as useObject, ObjectHook } from './useObject';
44
export { default as useObjectVal, ObjectValHook } from './useObjectVal';

database/useListKeys.ts

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

database/useListVal.ts renamed to database/useListVals.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { database } from 'firebase';
22
import useList from './useList';
33

4-
export type ListValHook<T> = {
4+
export type ListValsHook<T> = {
55
error?: Object;
66
loading: boolean;
77
value: T[];
@@ -10,7 +10,7 @@ export type ListValHook<T> = {
1010
export default <T>(
1111
query: database.Query,
1212
keyField?: string
13-
): ListValHook<T> => {
13+
): ListValsHook<T> => {
1414
const { error, loading, value } = useList(query);
1515
return {
1616
error,

0 commit comments

Comments
 (0)