Skip to content

Commit

Permalink
fix: rename useCollection* hooks to useQuery* (#198)
Browse files Browse the repository at this point in the history
Existing hooks are deprecated and will be removed with
the next major version update
  • Loading branch information
andipaetzold committed Feb 24, 2023
1 parent e3a832d commit 47e58ef
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 236 deletions.
140 changes: 80 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ This library consists of 6 modules with many hooks:
- [`useDocumentOnce`](#useDocumentOnce)
- [`useQueries`](#useQueries)
- [`useQueriesData`](#useQueriesData)
- [`useQuery`](#useQuery)
- [`useQueryData`](#useQueryData)
- [`useQueryDataOnce`](#useQueryDataOnce)
- [`useQueryOnce`](#useQueryOnce)
- [`messaging`](#Messaging)
- [`useMessagingToken`](#useMessagingToken)
- [`storage`](#Storage)
Expand Down Expand Up @@ -246,174 +250,190 @@ import { ... } from 'react-firehooks/firestore';

#### useCollection

Returns and updates a QuerySnapshot of a Firestore Query
Deprecated. Identical to [`useQuery`](#useQuery)

#### useCollectionData

Deprecated. Identical to [`useQueryData`](#useQueryData)

#### useCollectionDataOnce

Deprecated. Identical to [`useQueryDataOnce`](#useQueryDataOnce)

#### useCollectionOnce

Deprecated. Identical to [`useQueryOnce`](#useQueryOnce)

#### useCountFromServer

Returns the number of documents in the result set of of a Firestore Query. Does not update the count once initially calculated.

Requires firebase 9.11.0 or later.

```javascript
const [querySnap, loading, error] = useCollection(query, options);
const [count, loading, error] = useCountFromServer(query);
```

Params:

- `query`: Firestore query that will be subscribed to
- `options`: Options to configure the subscription
- `query`: Firestore query whose result set size is calculated

Returns:

- `value`: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `value`: Size of the result set; `undefined` if the result set size is currently being calculated, or an error occurred
- `loading`: `true` while calculating the result size set; `false` if the result size set was calculated successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionData
#### useDocument

Returns and updates a the document data of a Firestore Query
Returns and updates a DocumentSnapshot of a Firestore DocumentReference

```javascript
const [data, loading, error] = useCollectionData(query, options);
const [documentSnap, loading, error] = useDocument(documentReference, options);
```

Params:

- `query`: Firestore query that will be subscribed to
- `documentReference`: Firestore DocumentReference that will be subscribed to
- `options`: Options to configure the subscription

Returns:

- `value`: Query data; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `value`: DocumentSnapshot; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionDataOnce
#### useDocumentData

Returns the data of a Firestore Query. Does not update the data once initially fetched
Returns and updates the data of a Firestore DocumentReference

```javascript
const [data, loading, error] = useCollectionDataOnce(query, options);
const [data, loading, error] = useDocumentData(documentReference, options);
```

Params:

- `query`: Firestore query that will be fetched
- `options`: Options to configure how the query is fetched
- `documentReference`: Firestore DocumentReference that will subscribed to
- `options`: Options to configure the subscription

Returns:

- `value`: Query data; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `value`: Document data; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionOnce
#### useDocumentDataOnce

Returns the QuerySnapshot of a Firestore Query. Does not update the QuerySnapshot once initially fetched
Returns the data of a Firestore DocumentReference

```javascript
const [querySnap, loading, error] = useCollectionOnce(query, options);
const [documentSnap, loading, error] = useDocumentDataOnce(documentReference, options);
```

Params:

- `query`: Firestore query that will be fetched
- `options`: Options to configure how the query is fetched
- `documentReference`: Firestore DocumentReference that will be fetched
- `options`: Options to configure the document will be fetched

Returns:

- `value`: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `value`: Document data; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCountFromServer

Returns the number of documents in the result set of of a Firestore Query. Does not update the count once initially calculated.
#### useDocumentOnce

Requires firebase 9.11.0 or later.
Returns the DocumentSnapshot of a Firestore DocumentReference. Does not update the DocumentSnapshot once initially fetched

```javascript
const [count, loading, error] = useCountFromServer(query);
const [querySnap, loading, error] = useDocumentData(documentReference, options);
```

Params:

- `query`: Firestore query whose result set size is calculated
- `documentReference`: Firestore DocumentReference that will be fetched
- `options`: Options to configure how the document will be fetched

Returns:

- `value`: Size of the result set; `undefined` if the result set size is currently being calculated, or an error occurred
- `loading`: `true` while calculating the result size set; `false` if the result size set was calculated successfully or an error occurred
- `value`: DocumentSnapshot; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useDocument
#### useQuery

Returns and updates a DocumentSnapshot of a Firestore DocumentReference
Returns and updates a QuerySnapshot of a Firestore Query

```javascript
const [documentSnap, loading, error] = useDocument(documentReference, options);
const [querySnap, loading, error] = useQuery(query, options);
```

Params:

- `documentReference`: Firestore DocumentReference that will be subscribed to
- `query`: Firestore query that will be subscribed to
- `options`: Options to configure the subscription

Returns:

- `value`: DocumentSnapshot; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `value`: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useDocumentData
#### useQueryData

Returns and updates the data of a Firestore DocumentReference
Returns and updates a the document data of a Firestore Query

```javascript
const [data, loading, error] = useDocumentData(documentReference, options);
const [data, loading, error] = useQueryData(query, options);
```

Params:

- `documentReference`: Firestore DocumentReference that will subscribed to
- `query`: Firestore query that will be subscribed to
- `options`: Options to configure the subscription

Returns:

- `value`: Document data; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `value`: Query data; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useDocumentDataOnce
#### useQueryDataOnce

Returns the data of a Firestore DocumentReference
Returns the data of a Firestore Query. Does not update the data once initially fetched

```javascript
const [documentSnap, loading, error] = useDocumentDataOnce(documentReference, options);
const [data, loading, error] = useQueryDataOnce(query, options);
```

Params:

- `documentReference`: Firestore DocumentReference that will be fetched
- `options`: Options to configure the document will be fetched
- `query`: Firestore query that will be fetched
- `options`: Options to configure how the query is fetched

Returns:

- `value`: Document data; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `value`: Query data; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useDocumentOnce
#### useQueryOnce

Returns the DocumentSnapshot of a Firestore DocumentReference. Does not update the DocumentSnapshot once initially fetched
Returns the QuerySnapshot of a Firestore Query. Does not update the QuerySnapshot once initially fetched

```javascript
const [querySnap, loading, error] = useDocumentData(documentReference, options);
const [querySnap, loading, error] = useQueryOnce(query, options);
```

Params:

- `documentReference`: Firestore DocumentReference that will be fetched
- `options`: Options to configure how the document will be fetched
- `query`: Firestore query that will be fetched
- `options`: Options to configure how the query is fetched

Returns:

- `value`: DocumentSnapshot; `undefined` if document does not exist, is currently being fetched, or an error occurred
- `loading`: `true` while fetching the document; `false` if the document was fetched successfully or an error occurred
- `value`: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

## useQueries
Expand Down
4 changes: 4 additions & 0 deletions src/firestore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export * from "./useDocumentDataOnce.js";
export * from "./useDocumentOnce.js";
export * from "./useQueries.js";
export * from "./useQueriesData.js";
export * from "./useQuery.js";
export * from "./useQueryData.js";
export * from "./useQueryDataOnce.js";
export * from "./useQueryOnce.js";
52 changes: 5 additions & 47 deletions src/firestore/useCollection.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
import { DocumentData, FirestoreError, onSnapshot, Query, QuerySnapshot, SnapshotListenOptions } from "firebase/firestore";
import { useCallback } from "react";
import { ValueHookResult } from "../common/types.js";
import { useListen, UseListenOnChange } from "../internal/useListen.js";
import { LoadingState } from "../internal/useLoadingValue.js";
import { isQueryEqual } from "./internal.js";

export type UseCollectionResult<Value extends DocumentData = DocumentData> = ValueHookResult<
QuerySnapshot<Value>,
FirestoreError
>;

/**
* Options to configure the subscription
*/
export interface UseCollectionOptions {
snapshotListenOptions?: SnapshotListenOptions;
}

/**
* Returns and updates a QuerySnapshot of a Firestore Query
*
* @template Value Type of the collection data
* @param {Query<Value> | undefined | null} query Firestore query that will be subscribed to
* @param {?UseCollectionOptions} options Options to configure the subscription
* @returns {UseCollectionResult<Value>} QuerySnapshot, loading, and error
* * value: QuerySnapshot; `undefined` if query is currently being fetched, or an error occurred
* * loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useCollection<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionOptions
): UseCollectionResult<Value> {
const { snapshotListenOptions = {} } = options ?? {};

const onChange: UseListenOnChange<QuerySnapshot<Value>, FirestoreError, Query<Value>> = useCallback(
(stableQuery, next, error) =>
onSnapshot<Value>(stableQuery, snapshotListenOptions, {
next,
error,
}),
[]
);

return useListen(query ?? undefined, onChange, isQueryEqual, LoadingState);
}
export {
useQuery as useCollection,
UseQueryOptions as UseCollectionOptions,
UseQueryResult as UseCollectionResult,
} from "./useQuery.js";
52 changes: 5 additions & 47 deletions src/firestore/useCollectionData.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
import { DocumentData, FirestoreError, onSnapshot, Query, SnapshotListenOptions, SnapshotOptions } from "firebase/firestore";
import { useCallback } from "react";
import { ValueHookResult } from "../common/types.js";
import { useListen, UseListenOnChange } from "../internal/useListen.js";
import { LoadingState } from "../internal/useLoadingValue.js";
import { isQueryEqual } from "./internal.js";

export type UseCollectionDataResult<Value extends DocumentData = DocumentData> = ValueHookResult<Value[], FirestoreError>;

/**
* Options to configure the subscription
*/
export interface UseCollectionDataOptions<Value extends DocumentData = DocumentData> {
snapshotListenOptions?: SnapshotListenOptions;
snapshotOptions?: SnapshotOptions;
initialValue?: Value[];
}

/**
* Returns and updates a the document data of a Firestore Query
*
* @template Value Type of the collection data
* @param {Query<Value> | undefined | null} query Firestore query that will be subscribed to
* @param {?UseCollectionDataOptions} options Options to configure the subscription
* * `initialValue`: Value that is returned while the query is being fetched.
* @returns {UseCollectionDataResult<Value>} Query data, loading state, and error
* * value: Query data; `undefined` if query is currently being fetched, or an error occurred
* * loading: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
* * error: `undefined` if no error occurred
*/
export function useCollectionData<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionDataOptions<Value>
): UseCollectionDataResult<Value> {
const { snapshotListenOptions = {}, snapshotOptions = {} } = options ?? {};

const onChange: UseListenOnChange<Value[], FirestoreError, Query<Value>> = useCallback(
(stableQuery, next, error) =>
onSnapshot<Value>(stableQuery, snapshotListenOptions, {
next: (snap) => next(snap.docs.map((doc) => doc.data(snapshotOptions))),
error,
}),
[]
);

return useListen(query ?? undefined, onChange, isQueryEqual, options?.initialValue ?? LoadingState);
}
export {
useQueryData as useCollectionData,
UseQueryDataOptions as UseCollectionDataOptions,
UseQueryDataResult as UseCollectionDataResult,
} from "./useQueryData.js";
Loading

0 comments on commit 47e58ef

Please sign in to comment.