You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/browser.md
+26-22Lines changed: 26 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -234,31 +234,13 @@ export default function SavedDraft() {
234
234
235
235
---
236
236
237
-
### Conditionally rendering in the browser {/*conditionally-rendering-in-the-browser*/}
237
+
### Conditionally rendering on the server {/*conditionally-rendering-in-the-browser*/}
238
238
239
-
Like other calls to [`use`](/reference/react/use), you can call `use(browser())` conditionally or inside a custom Hook. For example, you can wrap a Suspense-enabled data-fetching library's `useQuery`and skip server rendering when initial data is missing:
239
+
Like other calls to [`use`](/reference/react/use), you can call `use(browser())` conditionally or inside a custom Hook. For example, a `useTimeZone` Hook can accept an initial time zone when one is available and read it from the device when it is not.
240
240
241
-
```js {3}
242
-
functionuseBrowserQuery(query, options) {
243
-
if (options.initialData===undefined) {
244
-
use(browser('useBrowserQuery: No initial data was provided.'));
245
-
}
241
+
If an initial time zone is provided, `useTimeZone` includes it in the HTML. Otherwise, `use(browser())` leaves the closest [`<Suspense>`](/reference/react/Suspense) boundary's fallback in the HTML. In the browser, `use(browser())` returns `undefined`, so `useTimeZone` reads the user's time zone from the device.
On the server, `useBrowserQuery` calls `useQuery` only when `initialData` is available. Otherwise, the closest Suspense boundary's fallback remains in the HTML. In the browser, `use(browser())` returns `undefined`, so the query library can fetch the data or read it from its client cache.
260
-
261
-
The following example uses this pattern with time zones. The event time zone is provided as initial data, while the user's time zone is read from the browser. Click **Reload** to see the loading fallback for the user's time zone.
243
+
Click **Reload** to see the loading fallback before the user's time zone appears.
262
244
263
245
<Sandpack>
264
246
@@ -398,6 +380,28 @@ iframe {
398
380
399
381
</Sandpack>
400
382
383
+
You can apply the same pattern to a Suspense-enabled data-fetching library. For example, a wrapper can call `use(browser())` before the library's `useQuery` when initial data is missing:
384
+
385
+
```js {3}
386
+
functionuseBrowserQuery(query, options) {
387
+
if (options.initialData===undefined) {
388
+
use(browser('useBrowserQuery: No initial data was provided.'));
During server rendering, `useBrowserQuery` calls `useQuery` only when `initialData` is available. Otherwise, the closest Suspense boundary's fallback remains in the HTML. In the browser, `use(browser())` returns `undefined`, so the query library can fetch the data or read it from its client cache.
404
+
401
405
---
402
406
403
407
### Reporting browser-only rendering on the server {/*reporting-browser-only-rendering-on-the-server*/}
0 commit comments