Skip to content

fix(react): useRxDocument initial loading state is true when collection and key are provided - #8999

Open
dikshit-n wants to merge 1 commit into
pubkey:masterfrom
dikshit-n:fix/use-rx-document-initial-loading-state
Open

fix(react): useRxDocument initial loading state is true when collection and key are provided#8999
dikshit-n wants to merge 1 commit into
pubkey:masterfrom
dikshit-n:fix/use-rx-document-initial-loading-state

Conversation

@dikshit-n

Copy link
Copy Markdown

Summary

Fixes useRxDocument starting with loading: false on first render even when a valid collection and primaryKey are provided, causing a false "not found" flash before the subscription resolves.

Problem

In useRxDocument, the loading state was initialized to false:

const [loading, setLoading] = useState(false);

But setLoading(true) only runs inside useEffect. The first committed React paint always shows loading: false and result: null, which is indistinguishable from "document not found".

This was reported in #8965.

Solution

Initialize loading to true when collection is not null and primaryKey is not undefined:

const [loading, setLoading] = useState(collection !== null && primaryKey !== undefined);

This matches the established pattern in useRxQuery and useLiveRxQuery, both of which start with loading: true when valid inputs are provided.

Changes Made

  • src/plugins/react/hooks/use-rx-document.ts: Initialize loading state based on whether valid inputs are provided
  • test/react/react-hooks.test.tsx: Add 5 tests for useRxDocument covering loading state and result behavior
  • orga/changelog/fix-use-rx-document-initial-loading-state.md: Changelog entry

Testing

  • Unit tests added: 5 new test cases in test/react/react-hooks.test.tsx
  • Tests added following existing patterns (renderHook, waitFor, assert.strictEqual)

Checklist

  • Tests added following existing repo patterns
  • Code passes local test suite (CI will verify)
  • Lint passes (CI will verify)
  • Code follows repo style
  • Documentation updated (changelog entry added per CLAUDE.md)
  • No console.log or debug code left
  • No unrelated changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant