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
**The `browser` API is currently only available in React’s Canary and Experimental channels.**
@@ -11,12 +13,10 @@ version: canary
11
13
12
14
</Canary>
13
15
14
-
<Intro>
15
-
16
-
`browser` lets you skip rendering part of a React tree on the server, leaving its nearest Suspense fallback in place until that content renders in the browser.
16
+
`browser` lets you render part of a React tree only in the browser.
17
17
18
18
```js
19
-
use(browser(reason?));
19
+
use(browser(reason?))
20
20
```
21
21
22
22
</Intro>
@@ -41,24 +41,24 @@ function BrowserOnly() {
41
41
}
42
42
```
43
43
44
-
During server rendering, `use(browser())` stops rendering the component and renders the fallback of the closest [`<Suspense>`](/reference/react/Suspense) boundary instead. In the browser, it has no effect, so the component renders normally.
44
+
During server rendering, `use(browser())` stops rendering the component and leaves the closest [`<Suspense>`](/reference/react/Suspense) boundary's fallback in its place. In the browser, `use(browser())` returns `undefined`, so the component renders normally.
45
45
46
46
[See more examples below.](#usage)
47
47
48
48
#### Parameters {/*parameters*/}
49
49
50
-
* **optional** `reason`: A string or function that provides diagnostic information about why rendering should happen only in the browser. React calls a reason functioneach time a server renderer encounters the value returned by `browser`; it never calls it in the browser. Use a function for values that are expensive to create, such as `() =>newError(...)`. The resulting value becomes the `cause` of the `Error` passed to `onBrowserBailout`.
50
+
* **optional** `reason`: A string or function that explains why the content needs to render in the browser. If you pass a function, React calls it each time a server renderer encounters the value returned by `browser`. React does not call it in the browser. Use a function for values that are expensive to create, such as `() =>newError(...)`. The string or the function's return value becomes the `cause` of the `Error` passed to `onBrowserBailout`.
51
51
52
52
#### Returns {/*returns*/}
53
53
54
-
`browser` returns an opaque value. Pass this value to `use` in a component, or use it as the reason when [aborting a server render](#aborting-pending-server-rendering-for-the-browser). In the browser, passing this value to `use` returns `undefined`.
54
+
`browser` returns a value that you can pass to `use` in a component or use as the reason when [aborting a server render](#aborting-pending-server-rendering-for-the-browser). In the browser, passing this value to `use` returns `undefined`.
55
55
56
56
#### Caveats {/*caveats*/}
57
57
58
-
* A component that passes a value returned by `browser` to `use` during server rendering must have a `<Suspense>` boundary above it. Otherwise, the entire server render will fail.
59
-
* `browser` is not available in a `react-server` environment. You can use it while server-rendering Client Components, but you cannot import it in a [React Server Component](/reference/rsc/server-components).
60
-
* Calling `browser()` by itself does not check the current environment or affect rendering. To trigger its behavior, pass the return value to `use` or use it to abort a server render. This means you can create the value at module scope and reuse it.
61
-
* To defer a component, pass the value returned by `browser` to `use`. Do not throw the value directly.
58
+
* `use(browser())`must be inside a `<Suspense>` boundary during server rendering. Without one, the server render fails.
59
+
* `browser` is not available in a `react-server` environment. You can use it while rendering Client Components on the server, but you cannot import it in a [React Server Component](/reference/rsc/server-components).
60
+
* Calling `browser()` by itself has no effect. You can create the value at module scope and reuse it.
61
+
* To skip rendering a component on the server, pass the value returned by `browser` to `use`. Do not throw it.
62
62
63
63
---
64
64
@@ -68,16 +68,20 @@ During server rendering, `use(browser())` stops rendering the component and rend
68
68
69
69
Call `use` with the value returned by `browser` to skip rendering a component on the server:
70
70
71
-
```js
71
+
Press **Render on the server** to see the fallback first. The demo waits briefly before hydrating and showing the browser-only editor.
@@ -86,13 +90,122 @@ export default function Page() {
86
90
}
87
91
```
88
92
89
-
During server rendering, React includes the `Loading editor...` fallback in the HTML. When the app renders in the browser, `use(browser())` continues immediately and React renders the `Editor` instead.
In a React Server Components app, `use(browser())` must be called from a Client Component. If your framework uses Server Components by default, add the [`'use client'`](/reference/rsc/use-client) directive to that file or move the call to a child Client Component:
### Conditionally rendering in the browser {/*conditionally-rendering-in-the-browser*/}
94
207
95
-
Like other calls to [`use`](/reference/react/use), `use(browser())`can be called conditionally, including inside a custom Hook. For example, you can wrap a Suspense-enabled data-fetching library's `useQuery`to render initial data on the server, but defer to the browser when that data is missing:
208
+
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:
96
209
97
210
```js {3}
98
211
functionuseBrowserQuery(query, options) {
@@ -112,13 +225,13 @@ function ProductDetails({ productId, initialData }) {
112
225
}
113
226
```
114
227
115
-
On the server, `useBrowserQuery` calls the underlying `useQuery` only when `initialData` is available. Otherwise, `use(browser())` leaves the nearest Suspense fallback in the HTML. In the browser, `use(browser())`continues immediately, so the query library can fetch the data or read it from its client cache.
228
+
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.
116
229
117
230
---
118
231
119
232
### Reporting browser-only rendering on the server {/*reporting-browser-only-rendering-on-the-server*/}
120
233
121
-
Provide `onBrowserBailout` to the server renderer to report browser-only rendering. React does not report a browser-only render recovered by a Suspense boundary to the server renderer's `onError` callback or [`hydrateRoot`'s `onRecoverableError`](/reference/react-dom/client/hydrateRoot#error-logging-in-production) callback. This example also passes an optional reason, which React makes available as the reported error's `cause`:
234
+
Pass an `onBrowserBailout`callback to the server renderer to report browser-only rendering. When React leaves a Suspense fallback for the browser, it does not call the server renderer's `onError` callback or [`hydrateRoot`'s `onRecoverableError`](/reference/react-dom/client/hydrateRoot#error-logging-in-production) callback. This example also passes a reason, which is available as the reported error's `cause`:
1. An `Error` describing the browser-only render. If a reason was supplied to `browser`, it is available as the error's `cause`.
151
-
2. An `errorInfo` object containing the`componentStack`of the browser-only render.
263
+
1. An `Error` describing the browser-only render. If you passed a reason to `browser`, it is available as the error's `cause`.
264
+
2. An `errorInfo` object with a`componentStack`showing where browser-only rendering occurred.
152
265
153
-
The reason function can return any value. Returning a new `Error`gives the cause its own stack without creating that`Error` during rendering in the browser. React does not serialize the reason into the HTML.
266
+
The reason function can return any value. Return a new `Error`to give the cause its own stack without creating the`Error` in the browser. React does not serialize the reason into the HTML.
154
267
155
-
If there is no Suspense boundary to provide a fallback, the server render fails. React reports the failure through the renderer's normal error callbacks instead of `onBrowserBailout`.
268
+
If there is no Suspense boundary to provide a fallback, the server render fails. React reports the failure through the renderer's usual error callbacks instead of `onBrowserBailout`.
156
269
157
270
---
158
271
159
272
### Aborting pending server rendering for the browser {/*aborting-pending-server-rendering-for-the-browser*/}
160
273
161
-
You can pass the value returned by `browser` as the reason for aborting a server render. This leaves pending Suspense boundaries in their fallback state so React can render their content in the browser:
274
+
Pass the value returned by `browser` as the reason when aborting a server render. React then leaves pending Suspense boundaries in their fallback state and renders their content in the browser:
Unlike other abort reasons, a value returned by `browser`is not reported to the server renderer's `onError` callback or to `hydrateRoot`'s `onRecoverableError` callback. The server renderer reports each recovered Suspense boundary to `onBrowserBailout` instead.
290
+
A `browser`abort reason does not trigger the server renderer's `onError` callback or `hydrateRoot`'s `onRecoverableError` callback. Instead, the server renderer reports each recovered Suspense boundary to `onBrowserBailout`.
178
291
179
292
For server rendering APIs that accept an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal), pass `browser()` as the reason to [`AbortController.abort`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort).
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ These APIs can be used to make apps faster by pre-loading resources such as scri
34
34
35
35
This API controls how components render on the server:
36
36
37
-
* <CanaryBadge /> [`browser`](/reference/react-dom/browser) lets you skip rendering part of a React tree on the server, leaving its nearest Suspense fallback in place until that content renders in the browser.
37
+
* <CanaryBadge /> [`browser`](/reference/react-dom/browser) lets you render part of a React tree only in the browser.
0 commit comments