Skip to content

Commit

Permalink
Fix also useQueries, update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
steida committed Apr 30, 2024
1 parent 6d08387 commit cea02b6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-pants-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@evolu/common-react": patch
---

Fix also useQueries, update comments
4 changes: 3 additions & 1 deletion packages/evolu-common-react/src/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export const useQueries = <
const once = useRef(options).current.once;
const allQueries = once ? queries.concat(once) : queries;
const wasSSR = useWasSSR();
if (!wasSSR) {
if (wasSSR) {
if (!options.promises) evolu.loadQueries(allQueries);
} else {
if (options.promises) options.promises.map(use);
else evolu.loadQueries(allQueries).map(use);
}
Expand Down
13 changes: 2 additions & 11 deletions packages/evolu-common-react/src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import { useWasSSR } from "./useWasSSR.js";
* // Get all rows, but without subscribing to changes.
* const { rows } = useQuery(allTodos, { once: true });
*
* // Prefetch all rows
* // Prefetch rows.
* const allTodos = evolu.createQuery((db) =>
* db.selectFrom("todo").selectAll(),
* );
* // Load before usage.
* const allTodosPromise = evolu.loadQuery(allTodos);
* // A usage.
* // Use prefetched rows.
* const { rows } = useQuery(allTodos, { promise: allTodosPromise });
*/
export const useQuery = <R extends Row>(
Expand All @@ -43,15 +42,7 @@ export const useQuery = <R extends Row>(
): QueryResult<R> => {
const evolu = useEvolu();
const wasSSR = useWasSSR();
/**
* `wasSSR` is for correct hydrating without Suspense flashing. For the
* initial render, it returns true and nothing else.
*/
if (wasSSR) {
/**
* On the server, the loadQuery is a no-op. On the client, it preloads the
* query and updates its subscription, which invokes a second render phase.
*/
if (!options?.promise) evolu.loadQuery(query);
} else {
use(options?.promise || evolu.loadQuery(query));
Expand Down
2 changes: 1 addition & 1 deletion packages/evolu-common-react/src/useWasSSR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const emptySubscribe = () => () => {};
* "Unlike the typeof window hack, this ensures that the server and hydration
* sees the same thing."
*
* The magic ingredient is useRef; check Sebastian's tweet.
* https://twitter.com/sebmarkbage/status/1763640725088923668
* https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store
*/
export const useWasSSR = (): boolean => {
const ref = useRef(false);
Expand Down

0 comments on commit cea02b6

Please sign in to comment.