Skip to content
Source
Sign inSign up
TanStack/db

fix(react-db): idiomatic `useLiveQuery` & `useLiveSuspenseQuery` hooks #1721

Closedzeorin(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/1721/headopened

Showing 3 changed files with 71 additions and 53 deletions (loading 1).

x
0 of 3 viewed, 3 left
12 additions and 0 deletions.changeset/eleven-gifts-shine.mdAdded
View file
@@ -0,0 +1,12 @@
+---
+'@tanstack/react-db': patch
+---
+​
+fix(react-db): Avoid using refs to track changes in React hooks.
+​
+Using refs to track previous versions of variables, and reading those refs
+during render, is an anti-pattern that breaks the Rules of Hooks and may lead to
+subtle bugs, especially in concurrent mode.
+​
+Using state instead is more idiomatic and ensures there will be no state
+tearing, even during concurrent mode updates.
59 additions and 53 deletionspackages/react-db/src/useLiveQuery.tsModified
View file
@@ -1,4 +1,4 @@
-import { useRef, useSyncExternalStore } from 'react'
+import { useCallback, useState, useSyncExternalStore } from 'react'
import {
BaseQueryBuilder,
createLiveQueryCollection,
@@ -13,7 +13,6 @@
InferResultType,
InitialQueryBuilder,
LiveQueryCollectionConfig,