feat: add keepPreviousData option to eliminate UI flash during transitions #68
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
keepPreviousDataoption (default:true) touseSubscribethat preserves previous subscription data during dependency transitions, eliminating the UI flash that occurs when switching between subscriptions.Problem
When
useSubscribedependencies change (e.g., navigating/list/AAA→/list/BBB), users experience a brief flash where the default value is shown before new data arrives:History
setSnapshot(def)setSnapshot(undefined)unstable_batchedUpdatesThe flash was introduced in v4.0.0 (PR #57) as a side effect of type safety improvements, not as an intentional UX decision.
Solution
Behavior:
keepPreviousData: true(default): Previous data stays visible until new subscription fireskeepPreviousData: false: Immediately shows default value (v4.0.0+ behavior)Implementation Details
prevSnapshotRef: Tracks the most recent successful subscription dataisMountedguard: Prevents setState after component unmounthasRunEffectRef: Distinguishes initial mount from dependency transitionsBreaking Change
This changes the default behavior from v4.0.0+. Users who relied on seeing the default value during transitions can opt-out:
Why Default to
truekeepPreviousDatafor UXTest Coverage
keepPreviousData: truepreserves data across transitionskeepPreviousData: falseresets to defaultRelated
Partially addresses #19 (Support suspense in useSubscribe) by eliminating the primary use case for Suspense—avoiding flash during transitions—without requiring Suspense boundaries.
Published as
@renchris/replicache-react@6.1.0for testing.