Skip to content

Commit 64c6968

Browse files
committed
Polish conditional browser examples
1 parent 20301b3 commit 64c6968

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ export default function SavedDraft() {
236236
237237
### 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. Unlike Hooks, `use` can be called after a conditional return or directly inside a conditional statement. This lets a Component or custom Hook opt out of server rendering based on a condition, such as the value of a prop passed to it.
239+
Unlike Hooks, [`use`](/reference/react/use) can be called inside a conditional statement or after an early return.
240+
241+
You can use this behavior with `use(browser())` to conditionally opt a Component out of server rendering, including from inside a custom Hook. The condition might depend on the value of a prop.
240242
241243
For example, this `useTimeZone` Hook accepts an optional default value. When provided, React renders the default value in the initial HTML and in the browser. Without a default value, the Component suspends during server rendering and shows the device's local time zone in the browser.
242244
@@ -400,7 +402,11 @@ function ProductDetails({ productId, initialData }) {
400402
}
401403
```
402404
403-
If `initialData` is not provided, `useBrowserQuery` skips server rendering and leaves the closest [`<Suspense>`](/reference/react/Suspense) boundary's fallback in the HTML. In the browser, `useBrowserQuery` calls `useQuery`, allowing the query library to fetch the data or read it from its client cache as usual.
405+
When `initialData` is provided, `useBrowserQuery` calls `useQuery` during server rendering, and React includes the rendered content in the HTML.
406+
407+
Without `initialData`, `useBrowserQuery` calls `use(browser())`, suspending the Component and leaving the closest [`<Suspense>`](/reference/react/Suspense) boundary's fallback in the HTML.
408+
409+
In the browser, `useBrowserQuery` calls `useQuery` in both cases, allowing the query library to fetch the data or read it from its client cache as usual.
404410
405411
---
406412
@@ -419,19 +425,22 @@ function SavedDraft() {
419425
return <DraftEditor initialDraft={draft} />;
420426
}
421427

422-
const { pipe } = renderToPipeableStream(
423-
<Suspense fallback={<p>Loading saved draft...</p>}>
424-
<SavedDraft />
425-
</Suspense>,
426-
{
427-
onShellReady() {
428-
pipe(response);
429-
},
430-
onBrowserBailout(error, errorInfo) {
431-
logBrowserBailout(error, errorInfo);
432-
}
428+
function App() {
429+
return (
430+
<Suspense fallback={<p>Loading saved draft...</p>}>
431+
<SavedDraft />
432+
</Suspense>
433+
);
434+
}
435+
436+
const { pipe } = renderToPipeableStream(<App />, {
437+
onShellReady() {
438+
pipe(response);
439+
},
440+
onBrowserBailout(error, errorInfo) {
441+
logBrowserBailout(error, errorInfo);
433442
}
434-
);
443+
});
435444
```
436445
437446
`onBrowserBailout` receives two arguments:

0 commit comments

Comments
 (0)