Skip to content

Commit b14b97a

Browse files
committed
Clarify browser-only rendering guidance
1 parent 96ad418 commit b14b97a

5 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/content/reference/react-dom/client/hydrateRoot.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ export default function App() {
319319
320320
This way the initial render pass will render the same content as the server, avoiding mismatches, but an additional pass will happen synchronously right after hydration.
321321
322+
This approach is useful when you need different content on the server and in the browser. If a component should not render on the server at all, React Canary's [`browser`](/reference/react-dom/browser) API lets you mark it as browser-only without waiting for an Effect.
323+
322324
<Pitfall>
323325
324326
This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections. The JavaScript code may load significantly later than the initial HTML render, so rendering a different UI immediately after hydration may also feel jarring to the user.

src/content/reference/react/Suspense.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,8 @@ function Chat() {
23722372
23732373
The server HTML will include the loading indicator. It will be replaced by the `Chat` component on the client.
23742374
2375+
In React Canary, call [`use(browser())`](/reference/react/use#use-browser) instead of throwing an error to mark a component as browser-only.
2376+
23752377
---
23762378
23772379
### <CanaryBadge /> Providing a fallback for browser-only content {/*providing-a-fallback-for-browser-only-content*/}

src/content/reference/react/apis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ In addition to [Hooks](/reference/react/hooks) and [Components](/reference/react
2727

2828
To read a value from a resource, use this API:
2929

30-
* [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
30+
* [`use`](/reference/react/use) lets you read the value of a resource like a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context). In Canary, you can also pass it the value returned by [`browser`](/reference/react-dom/browser) to mark a component as browser-only during server rendering.
3131
```js
3232
function MessageComponent({ messagePromise }) {
3333
const message = use(messagePromise);

src/content/reference/react/use.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: use
44

55
<Intro>
66

7-
`use` is a React API that lets you read the value of a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context).
7+
`use` is a React API that lets you read the value of a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or [context](/learn/passing-data-deeply-with-context). In Canary, you can also pass `use` the value returned by [`browser`](/reference/react-dom/browser) to mark a component as browser-only during server rendering.
88

99
```js
1010
const value = use(resource);
@@ -1326,6 +1326,8 @@ async function getData(url) {
13261326
13271327
---
13281328
1329+
## Usage (`browser`) {/*usage-browser*/}
1330+
13291331
### <CanaryBadge /> Rendering a component only in the browser {/*rendering-a-component-only-in-the-browser*/}
13301332
13311333
Pass the value returned by [`browser`](/reference/react-dom/browser) to `use` inside a component that should only render in the browser.

src/content/reference/react/useLayoutEffect.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ However, if you're running into this problem, you have a few different options:
734734
735735
- Replace `useLayoutEffect` with [`useEffect`.](/reference/react/useEffect) This tells React that it's okay to display the initial render result without blocking the paint (because the original HTML will become visible before your Effect runs).
736736
737-
- Alternatively, [mark your component as client-only.](/reference/react/Suspense#providing-a-fallback-for-server-errors-and-client-only-content) This tells React to replace its content up to the closest [`<Suspense>`](/reference/react/Suspense) boundary with a loading fallback (for example, a spinner or a glimmer) during server rendering.
737+
- Alternatively, [mark your component as client-only.](/reference/react/Suspense#providing-a-fallback-for-server-errors-and-client-only-content) This tells React to replace its content up to the closest [`<Suspense>`](/reference/react/Suspense) boundary with a loading fallback (for example, a spinner or a glimmer) during server rendering. In React Canary, use the [`browser`](/reference/react-dom/browser) API instead of throwing an error to mark the component as browser-only.
738738
739739
- Alternatively, you can render a component with `useLayoutEffect` only after hydration. Keep a boolean `isMounted` state that's initialized to `false`, and set it to `true` inside a `useEffect` call. Your rendering logic can then be like `return isMounted ? <RealContent /> : <FallbackContent />`. On the server and during the hydration, the user will see `FallbackContent` which should not call `useLayoutEffect`. Then React will replace it with `RealContent` which runs on the client only and can include `useLayoutEffect` calls.
740740

0 commit comments

Comments
 (0)