Skip to content

Commit fa4a59c

Browse files
committed
Use time zone for conditional browser example
1 parent 44b7dd5 commit fa4a59c

1 file changed

Lines changed: 20 additions & 65 deletions

File tree

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

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -258,91 +258,46 @@ function ProductDetails({ productId, initialData }) {
258258
259259
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.
260260
261-
You can also use this pattern to read from a browser-only data source when initial data isn't available. In this example, the email setting has initial data, but the push setting is read from IndexedDB. Click **Reload** to see the loading fallback for the push setting.
261+
You can also call `use(browser())` only when initial data isn't available. In this example, the event time zone is provided as initial data, but the user's time zone is read from the browser. Click **Reload** to see the loading fallback for the user's time zone.
262262
263263
<Sandpack>
264264
265265
```js src/App.js
266266
import { Suspense } from 'react';
267-
import { useSetting } from './useSetting.js';
267+
import { useTimeZone } from './useTimeZone.js';
268268

269-
function NotificationSetting({settingId, label, initialValue}) {
270-
const enabled = useSetting(settingId, initialValue);
271-
return (
272-
<li>
273-
{label}: <strong>{enabled ? 'On' : 'Off'}</strong>
274-
</li>
275-
);
269+
function TimeZone({label, initialTimeZone}) {
270+
const timeZone = useTimeZone(initialTimeZone);
271+
return <p>{label}: <strong>{timeZone}</strong></p>;
276272
}
277273

278274
export default function App() {
279275
return (
280276
<>
281-
<h1>Notification settings</h1>
282-
<ul>
283-
<NotificationSetting
284-
settingId="email"
285-
label="Email notifications"
286-
initialValue={true}
287-
/>
288-
<Suspense fallback={<li>Loading push notification setting...</li>}>
289-
<NotificationSetting
290-
settingId="push"
291-
label="Push notifications"
292-
/>
293-
</Suspense>
294-
</ul>
277+
<h1>Event details</h1>
278+
<TimeZone
279+
label="Event time zone"
280+
initialTimeZone="America/New_York"
281+
/>
282+
<Suspense fallback={<p>Loading your time zone...</p>}>
283+
<TimeZone label="Your time zone" />
284+
</Suspense>
295285
</>
296286
);
297287
}
298288
```
299289
300-
```js src/useSetting.js active
290+
```js src/useTimeZone.js active
301291
import { use } from 'react';
302292
import { browser } from 'react-dom';
303-
import { readSetting } from './database.js';
304293

305-
export function useSetting(settingId, initialValue) {
306-
if (initialValue !== undefined) {
307-
return initialValue;
294+
export function useTimeZone(initialTimeZone) {
295+
if (initialTimeZone !== undefined) {
296+
return initialTimeZone;
308297
}
309298

310-
use(browser('No initial setting was provided.'));
311-
return use(readSetting(settingId));
312-
}
313-
```
314-
315-
```js src/database.js hidden
316-
// This is a simplified IndexedDB wrapper for this example.
317-
318-
const cache = new Map();
319-
320-
export function readSetting(settingId) {
321-
if (!cache.has(settingId)) {
322-
cache.set(settingId, readSettingFromIndexedDB(settingId));
323-
}
324-
return cache.get(settingId);
325-
}
326-
327-
function readSettingFromIndexedDB(settingId) {
328-
return new Promise((resolve, reject) => {
329-
const request = indexedDB.open('browser-notification-settings-example', 1);
330-
331-
request.onupgradeneeded = () => {
332-
const store = request.result.createObjectStore('settings');
333-
store.add(true, 'push');
334-
};
335-
336-
request.onerror = () => reject(request.error);
337-
request.onsuccess = () => {
338-
const transaction = request.result.transaction('settings');
339-
const settingRequest = transaction.objectStore('settings').get(settingId);
340-
settingRequest.onerror = () => reject(settingRequest.error);
341-
settingRequest.onsuccess = () => {
342-
setTimeout(() => resolve(settingRequest.result), 600);
343-
};
344-
};
345-
});
299+
use(browser('No initial time zone was provided.'));
300+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
346301
}
347302
```
348303
@@ -353,7 +308,7 @@ export default function Document() {
353308
return (
354309
<html lang="en">
355310
<head>
356-
<title>Notification settings</title>
311+
<title>Event details</title>
357312
<style>{`
358313
h1 { font-size: 24px; margin-top: 0; }
359314
`}</style>

0 commit comments

Comments
 (0)