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
* Add conditional browser query example
* Move browser query hook into its own example file
* Use IndexedDB in conditional browser example
* Clarify conditional browser rendering example
* Polish browser rendering examples
* Bridge conditional browser examples
* Tighten conditional browser example intro
* Use time zone for conditional browser example
* Clarify conditional example transition
* Lead conditional browser docs with time zone example
* Address conditional browser rendering feedback
* update
* Clarify conditional browser control flow
* Clarify conditional data fetching pattern
* Clarify conditional rendering outcomes
* Polish conditional browser examples
* Clarify conditional query rendering
* Align conditional browser guidance
@@ -234,9 +234,153 @@ 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-on-the-server*/}
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), `use(browser())` can be called inside a conditional statement or after an early return. This lets a Component or custom Hook opt out of server rendering based on a condition, such as the value of a prop.
240
+
241
+
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.
242
+
243
+
Click **Reload** to see the loading fallback before the user's time zone appears.
244
+
245
+
<Sandpack>
246
+
247
+
```js src/App.js
248
+
import { Suspense } from'react';
249
+
import { useTimeZone } from'./useTimeZone.js';
250
+
251
+
functionTimeZone({label, defaultTimeZone}) {
252
+
consttimeZone=useTimeZone(defaultTimeZone);
253
+
return<p>{label}:<strong>{timeZone}</strong></p>;
254
+
}
255
+
256
+
exportdefaultfunctionApp() {
257
+
return (
258
+
<>
259
+
<h1>Event details</h1>
260
+
<TimeZone
261
+
label="Event time zone"
262
+
defaultTimeZone="America/New_York"
263
+
/>
264
+
<Suspense fallback={<p>Loading your time zone...</p>}>
265
+
<TimeZone label="Your time zone"/>
266
+
</Suspense>
267
+
</>
268
+
);
269
+
}
270
+
```
271
+
272
+
```js src/useTimeZone.js active
273
+
import { use } from'react';
274
+
import { browser } from'react-dom';
275
+
276
+
exportfunctionuseTimeZone(defaultTimeZone) {
277
+
if (defaultTimeZone !==undefined) {
278
+
return defaultTimeZone;
279
+
}
280
+
281
+
use(browser('No default time zone was provided.'));
You can apply a similar pattern to conditionally avoid server rendering when using a Suspense-enabled data-fetching library:
240
384
241
385
```js {3}
242
386
functionuseBrowserQuery(query, options) {
@@ -256,7 +400,7 @@ function ProductDetails({ productId, initialData }) {
256
400
}
257
401
```
258
402
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.
403
+
With `initialData`, React renders the Component to HTML on the server. Without it, React leaves the closest [`<Suspense>`](/reference/react/Suspense) boundary's fallback in the HTML. In the browser, `useQuery`can fetch the data or read it from its client cache as usual.
0 commit comments