@instantdb/react-common 0.22.155 → 0.22.156

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/react-common",
3
- "version": "0.22.155",
3
+ "version": "0.22.156",
4
4
  "description": "Instant DB shared components for React and React Native",
5
5
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/react-common",
6
6
  "repository": {
@@ -53,8 +53,8 @@
53
53
  "react": ">=16"
54
54
  },
55
55
  "dependencies": {
56
- "@instantdb/core": "0.22.155",
57
- "@instantdb/version": "0.22.155"
56
+ "@instantdb/core": "0.22.156",
57
+ "@instantdb/version": "0.22.156"
58
58
  },
59
59
  "scripts": {
60
60
  "test": "vitest",
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';
2
2
  import type { InstantReactRoom } from './InstantReactRoom.ts';
3
+ import { useQueryInternal } from './useQuery.ts';
3
4
 
4
- export { InstantReactAbstractDatabase, type InstantReactRoom };
5
+ export {
6
+ InstantReactAbstractDatabase,
7
+ type InstantReactRoom,
8
+ useQueryInternal,
9
+ };
package/src/useQuery.ts CHANGED
@@ -1,3 +1,5 @@
1
+ 'use client';
2
+
1
3
  import {
2
4
  weakHash,
3
5
  coerceQuery,
@@ -34,9 +36,11 @@ export function useQueryInternal<
34
36
  _core: InstantCoreDatabase<Schema, UseDates>,
35
37
  _query: null | Q,
36
38
  _opts?: InstaQLOptions,
39
+ serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined,
37
40
  ): {
38
41
  state: InstaQLLifecycleState<Schema, Q, UseDates>;
39
42
  query: any;
43
+ queryHash: string;
40
44
  } {
41
45
  if (_query && _opts && 'ruleParams' in _opts) {
42
46
  _query = { $$ruleParams: _opts['ruleParams'], ..._query };
@@ -53,6 +57,12 @@ export function useQueryInternal<
53
57
  stateForResult(_core._reactor.getPreviousResult(query)),
54
58
  );
55
59
 
60
+ // Cache the server snapshot to satisfy useSyncExternalStore's requirement
61
+ // that getServerSnapshot returns the same reference across calls.
62
+ const serverSnapshotCacheRef = useRef<
63
+ InstaQLLifecycleState<Schema, Q, UseDates>
64
+ >(defaultState as any);
65
+
56
66
  // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
57
67
  // if `subscribe` changes, so we use `useCallback` to memoize the function.
58
68
  const subscribe = useCallback(
@@ -98,7 +108,17 @@ export function useQueryInternal<
98
108
  >(
99
109
  subscribe,
100
110
  () => resultCacheRef.current,
101
- () => defaultState,
111
+ () => {
112
+ if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {
113
+ const result = serverSnapshot();
114
+ if (result) {
115
+ serverSnapshotCacheRef.current = result;
116
+ }
117
+ }
118
+
119
+ return serverSnapshotCacheRef.current;
120
+ },
102
121
  );
103
- return { state, query };
122
+
123
+ return { state, query, queryHash };
104
124
  }