It provides a single hook useRead that allows you to fetch once or subscribe to a document or a collection updates:
The useRead hook accepts any reading method, both in promise and subscription mode. It returns a tuple with the result and the status object:
When running in subscription mode, the hook returns the latest data state and updates it when the document changes.
When the component is unmounted, the subscription is automatically unsubscribed.
Postponing requests
If you need to wait for something, i.e., for userId to resolve, you can pass a falsy value to the useRead:
It also works within queries as well:
Status
Along with the data, useRead returns a status object as the second element of the tuple.
The status object tells if the data is loading or if there was an error:
Return type
The all, query, and many methods return an array of documents or undefined if the query is not ready yet:
The get method returns a single document, undefined if the query is not ready yet, or null if the document is not found:
useLazyRead
The useLazyRead hook is similar to useRead, but it doesn’t perform the request or subscribe to the updates automatically. Instead, it returns a function that you can call to trigger the request:
Here’s an example of how to create the context:
You can also delay the request by passing false to the useLazyRead: