Skip to content

Commit 12a4415

Browse files
committed
Lead conditional browser docs with time zone example
1 parent 7574db7 commit 12a4415

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

src/content/reference/react-dom/browser.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,31 +234,13 @@ export default function SavedDraft() {
234234
235235
---
236236
237-
### Conditionally rendering in the browser {/*conditionally-rendering-in-the-browser*/}
237+
### Conditionally rendering on the server {/*conditionally-rendering-in-the-browser*/}
238238
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.
240240
241-
```js {3}
242-
function useBrowserQuery(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.
246242
247-
return useQuery(query, options);
248-
}
249-
250-
function ProductDetails({ productId, initialData }) {
251-
const product = useBrowserQuery(`/api/products/${productId}`, {
252-
initialData,
253-
});
254-
255-
return <h1>{product.name}</h1>;
256-
}
257-
```
258-
259-
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.
262244
263245
<Sandpack>
264246
@@ -398,6 +380,28 @@ iframe {
398380
399381
</Sandpack>
400382
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+
function useBrowserQuery(query, options) {
387+
if (options.initialData === undefined) {
388+
use(browser('useBrowserQuery: No initial data was provided.'));
389+
}
390+
391+
return useQuery(query, options);
392+
}
393+
394+
function ProductDetails({ productId, initialData }) {
395+
const product = useBrowserQuery(`/api/products/${productId}`, {
396+
initialData,
397+
});
398+
399+
return <h1>{product.name}</h1>;
400+
}
401+
```
402+
403+
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+
401405
---
402406
403407
### Reporting browser-only rendering on the server {/*reporting-browser-only-rendering-on-the-server*/}

0 commit comments

Comments
 (0)