Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ The `useCollectionData` hook takes the following parameters:
- `query`: (optional) `firebase.firestore.Query` for the data you would like to load
- `options`: (optional) `Object` with the following parameters:
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded

Returns:
Expand All @@ -140,6 +141,7 @@ The `useCollectionDataOnce` hook takes the following parameters:
- `options`: (optional) `Object` with the following parameters:
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.id` property.
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.

Returns:

Expand Down Expand Up @@ -225,6 +227,7 @@ The `useDocumentData` hook takes the following parameters:
- `reference`: (optional) `firebase.firestore.DocumentReference` for the data you would like to load
- `options`: (optional) `Object` with the following parameters:
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.
- `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded

Returns:
Expand All @@ -247,6 +250,7 @@ The `useDocumentDataOnce` hook takes the following parameters:
- `options`: (optional) `Object` with the following parameters:
- `getOptions`: (optional) `firebase.firestore.GetOptions` to customise how the collection is loaded
- `idField`: (optional) name of the field that should be populated with the `firebase.firestore.DocumentSnapshot.id` property.
- `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property.

Returns:

Expand Down
4 changes: 3 additions & 1 deletion firestore/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import firebase from 'firebase/app';

export const snapshotToData = (
snapshot: firebase.firestore.DocumentSnapshot,
idField?: string
idField?: string,
refField?: string,
) => {
if (!snapshot.exists) {
return undefined;
Expand All @@ -11,5 +12,6 @@ export const snapshotToData = (
return {
...snapshot.data(),
...(idField ? { [idField]: snapshot.id } : null),
...(refField ? { [refField]: snapshot.ref } : null),
};
};
4 changes: 4 additions & 0 deletions firestore/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare export function useCollectionData<T>(
query?: Query | null,
options?: {
idField?: string,
refField?: string,
snapshotListenOptions?: SnapshotListenOptions,
}
): CollectionDataHook<T>;
Expand All @@ -39,6 +40,7 @@ declare export function useCollectionDataOnce<T>(
options?: {
getOptions?: GetOptions,
idField?: string,
refField?: string,
}
): CollectionDataHook<T>;
declare export function useDocument(
Expand All @@ -57,6 +59,7 @@ declare export function useDocumentData<T>(
ref?: DocumentReference | null,
options?: {
idField?: string,
refField?: string,
snapshotListenOptions?: SnapshotListenOptions,
}
): DocumentDataHook<T>;
Expand All @@ -65,5 +68,6 @@ declare export function useDocumentDataOnce<T>(
options?: {
getOptions?: GetOptions,
idField?: string,
refField?: string,
}
): DocumentDataHook<T>;
6 changes: 4 additions & 2 deletions firestore/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ export const useCollectionData = <T>(
query?: firebase.firestore.Query | null,
options?: {
idField?: string;
refField?: string;
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
}
): CollectionDataHook<T> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
const snapshotListenOptions = options
? options.snapshotListenOptions
: undefined;
Expand All @@ -65,9 +67,9 @@ export const useCollectionData = <T>(
const values = useMemo(
() =>
(snapshots
? snapshots.docs.map((doc) => snapshotToData(doc, idField))
? snapshots.docs.map(doc => snapshotToData(doc, idField, refField))
: undefined) as T[],
[snapshots, idField]
[snapshots, idField, refField]
);

const resArray: CollectionDataHook<T> = [values, loading, error];
Expand Down
6 changes: 4 additions & 2 deletions firestore/useCollectionOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,21 @@ export const useCollectionDataOnce = <T>(
options?: {
getOptions?: firebase.firestore.GetOptions;
idField?: string;
refField?: string;
}
): CollectionDataOnceHook<T> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
const getOptions = options ? options.getOptions : undefined;
const [snapshots, loading, error] = useCollectionOnce<T>(query, {
getOptions,
});
const values = useMemo(
() =>
(snapshots
? snapshots.docs.map((doc) => snapshotToData(doc, idField))
? snapshots.docs.map((doc) => snapshotToData(doc, idField, refField))
: undefined) as T[],
[snapshots, idField]
[snapshots, idField, refField]
);
const resArray: CollectionDataOnceHook<T> = [values, loading, error];
return useMemo(() => resArray, resArray);
Expand Down
6 changes: 4 additions & 2 deletions firestore/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ export const useDocumentData = <T>(
docRef?: firebase.firestore.DocumentReference | null,
options?: {
idField?: string;
refField?: string;
snapshotListenOptions?: firebase.firestore.SnapshotListenOptions;
}
): DocumentDataHook<T> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
const snapshotListenOptions = options
? options.snapshotListenOptions
: undefined;
const [snapshot, loading, error] = useDocument<T>(docRef, {
snapshotListenOptions,
});
const value = useMemo(
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
[snapshot, idField]
() => (snapshot ? snapshotToData(snapshot, idField, refField) : undefined) as T,
[snapshot, idField, refField]
);

const resArray: DocumentDataHook<T> = [value, loading, error];
Expand Down
6 changes: 4 additions & 2 deletions firestore/useDocumentOnce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ export const useDocumentDataOnce = <T>(
options?: {
getOptions?: firebase.firestore.GetOptions;
idField?: string;
refField?: string;
}
): DocumentDataOnceHook<T> => {
const idField = options ? options.idField : undefined;
const refField = options ? options.refField : undefined;
const getOptions = options ? options.getOptions : undefined;
const [snapshot, loading, error] = useDocumentOnce<T>(docRef, { getOptions });
const value = useMemo(
() => (snapshot ? snapshotToData(snapshot, idField) : undefined) as T,
[snapshot, idField]
() => (snapshot ? snapshotToData(snapshot, idField, refField) : undefined) as T,
[snapshot, idField, refField]
);

const resArray: DocumentDataOnceHook<T> = [value, loading, error];
Expand Down