RefirebaseRefirebase

useDocument

Subscribe to a single Firestore document with real-time updates.

Usage

The useDocument hook listens to a single document in a Firestore collection and provides real-time updates.

React Component
import { useDocument } from 'refirebase/react';

function UserProfile({ userId }: { userId: string }) {
  const { data: user, loading, error } = useDocument('users', userId);

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error loading user.</p>;
  
  return <div>{user?.name}</div>;
}

Parameters

ParamTypeDescription
collectionName*stringThe name of the Firestore collection.
docId*stringThe ID of the document to subscribe to.

Return value

Returns

{ data: T | null, loading: boolean, error: unknown | null }

An object containing the document data, loading state, and any encountered error.