The useDocument
hook makes it simple to connect with realtime Firestore documents, even when paths are dynamic or depend on the ID of the current user. This hook is particularly useful in React applications where document paths depend on runtime values like user IDs or route parameters.
useDocument
is to provide a DocumentReference
to the hook. This will fetch the document and return its data. The hook will also automatically update the component with the latest data whenever the document changes.
DocumentReference
. The function should return a DocumentReference
and its first argument will be the user ID of the active user.
For example, here’s how we can fetch the active user’s own profile:
useEffect
hook.
For example, we might want to display a User
and their current Company
on the same page. In this case, the company document we want to connect to depends on the user document. This is a great use case for useDocument
:
companyId
is defined, Firebridge will create the company reference and fetch the company document.
Whether the user changes, or the company changes, or the user switches companies, Firebridge will automatically update the component with the latest data.