Skip to content

Commit

Permalink
feat: rename useCollection* hooks to useQuery* (#194)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `useCollection*` hooks are renamed to `useQuery*`
  • Loading branch information
andipaetzold committed Feb 10, 2023
1 parent 0c14476 commit 372f055
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ This library consists of 6 modules with many hooks:
- [`useObjectValue`](#useObjectValue)
- [`useObjectValueOnce`](#useObjectValueOnce)
- [`firestore`](#Firestore)
- [`useCollection`](#useCollection)
- [`useCollectionData`](#useCollectionData)
- [`useCollectionDataOnce`](#useCollectionDataOnce)
- [`useCollectionOnce`](#useCollectionOnce)
- [`useQuery`](#useQuery)
- [`useQueryData`](#useQueryData)
- [`useQueryDataOnce`](#useQueryDataOnce)
- [`useQueryOnce`](#useQueryOnce)
- [`useCountFromServer`](#useCountFromServer)
- [`useDocument`](#useDocument)
- [`useDocumentData`](#useDocumentData)
Expand Down Expand Up @@ -242,12 +242,12 @@ Returns:
import { ... } from 'react-firehooks/firestore';
```

#### useCollection
#### useQuery

Returns and updates a QuerySnapshot of a Firestore Query

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

Params:
Expand All @@ -261,12 +261,12 @@ Returns:
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionData
#### useQueryData

Returns and updates a the document data of a Firestore Query

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

Params:
Expand All @@ -280,12 +280,12 @@ Returns:
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionDataOnce
#### useQueryDataOnce

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

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

Params:
Expand All @@ -299,12 +299,12 @@ Returns:
- `loading`: `true` while fetching the query; `false` if the query was fetched successfully or an error occurred
- `error`: `undefined` if no error occurred

#### useCollectionOnce
#### useQueryOnce

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

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

Params:
Expand Down
8 changes: 4 additions & 4 deletions src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export * from "./types.js";
export * from "./useCollection.js";
export * from "./useCollectionData.js";
export * from "./useCollectionDataOnce.js";
export * from "./useCollectionOnce.js";
export * from "./useCountFromServer.js";
export * from "./useDocument.js";
export * from "./useDocumentData.js";
export * from "./useDocumentDataOnce.js";
export * from "./useDocumentOnce.js";
export * from "./useQuery.js";
export * from "./useQueryData.js";
export * from "./useQueryDataOnce.js";
export * from "./useQueryOnce.js";
14 changes: 7 additions & 7 deletions src/firestore/useCollection.ts → src/firestore/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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<
export type UseQueryResult<Value extends DocumentData = DocumentData> = ValueHookResult<
QuerySnapshot<Value>,
FirestoreError
>;

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

Expand All @@ -22,16 +22,16 @@ export interface UseCollectionOptions {
*
* @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
* @param {?UseQueryOptions} options Options to configure the subscription
* @returns {UseQueryResult<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>(
export function useQuery<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionOptions
): UseCollectionResult<Value> {
options?: UseQueryOptions
): UseQueryResult<Value> {
const { snapshotListenOptions = {} } = options ?? {};

const onChange: UseListenOnChange<QuerySnapshot<Value>, FirestoreError, Query<Value>> = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ 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>;
export type UseQueryDataResult<Value extends DocumentData = DocumentData> = ValueHookResult<Value[], FirestoreError>;

/**
* Options to configure the subscription
*/
export interface UseCollectionDataOptions<Value extends DocumentData = DocumentData> {
export interface UseQueryDataOptions<Value extends DocumentData = DocumentData> {
snapshotListenOptions?: SnapshotListenOptions;
snapshotOptions?: SnapshotOptions;
initialValue?: Value[];
Expand All @@ -21,17 +21,17 @@ export interface UseCollectionDataOptions<Value extends DocumentData = DocumentD
*
* @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
* @param {?UseQueryDataOptions} 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
* @returns {UseQueryDataResult<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>(
export function useQueryData<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionDataOptions<Value>
): UseCollectionDataResult<Value> {
options?: UseQueryDataOptions<Value>
): UseQueryDataResult<Value> {
const { snapshotListenOptions = {}, snapshotOptions = {} } = options ?? {};

const onChange: UseListenOnChange<Value[], FirestoreError, Query<Value>> = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import { useOnce } from "../internal/useOnce.js";
import { getDocsFromSource, isQueryEqual } from "./internal.js";
import type { Source } from "./types.js";

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

/**
* Options to configure the subscription
*/
export interface UseCollectionDataOnceOptions {
export interface UseQueryDataOnceOptions {
source?: Source;
snapshotOptions?: SnapshotOptions;
}
Expand All @@ -23,16 +20,16 @@ export interface UseCollectionDataOnceOptions {
*
* @template Value Type of the collection data
* @param {Query<Value> | undefined | null} query Firestore query that will be fetched
* @param {?UseCollectionDataOnceOptions} options Options to configure how the query is fetched
* @returns {UseCollectionDataOnceResult<Value>} Query data, loading state, and error
* @param {?UseQueryDataOnceOptions} options Options to configure how the query is fetched
* @returns {UseQueryDataOnceResult<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 useCollectionDataOnce<Value extends DocumentData = DocumentData>(
export function useQueryDataOnce<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionDataOnceOptions
): UseCollectionDataOnceResult<Value> {
options?: UseQueryDataOnceOptions
): UseQueryDataOnceResult<Value> {
const { source = "default", snapshotOptions = {} } = options ?? {};

const getData = useCallback(async (stableQuery: Query<Value>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { useOnce } from "../internal/useOnce.js";
import { getDocsFromSource, isQueryEqual } from "./internal.js";
import type { Source } from "./types.js";

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

/**
* Options to configure how the query is fetched
*/
export interface UseCollectionOnceOptions {
export interface UseQueryOnceOptions {
source?: Source;
}

Expand All @@ -22,16 +22,16 @@ export interface UseCollectionOnceOptions {
*
* @template Value Type of the collection data
* @param {Query<Value> | undefined | null} query Firestore query that will be fetched
* @param {?UseCollectionOnceOptions} options Options to configure how the query is fetched
* @returns {UseCollectionOnceResult<Value>} QuerySnapshot, loading state, and error
* @param {?UseQueryOnceOptions} options Options to configure how the query is fetched
* @returns {UseQueryOnceResult<Value>} QuerySnapshot, loading state, 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 useCollectionOnce<Value extends DocumentData = DocumentData>(
export function useQueryOnce<Value extends DocumentData = DocumentData>(
query: Query<Value> | undefined | null,
options?: UseCollectionOnceOptions
): UseCollectionOnceResult<Value> {
options?: UseQueryOnceOptions
): UseQueryOnceResult<Value> {
const { source = "default" } = options ?? {};

const getData = useCallback(async (stableQuery: Query<Value>) => getDocsFromSource(stableQuery, source), []);
Expand Down

0 comments on commit 372f055

Please sign in to comment.