Skip to content

Commit 262b84a

Browse files
committed
Use IndexedDB in conditional browser example
1 parent 9247c01 commit 262b84a

1 file changed

Lines changed: 63 additions & 62 deletions

File tree

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

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -236,109 +236,110 @@ export default function SavedDraft() {
236236
237237
### Conditionally rendering in the browser {/*conditionally-rendering-in-the-browser*/}
238238
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), you can call `use(browser())` conditionally or inside a custom Hook. For example, a custom Hook can use initial data when it is available during server rendering, and read from IndexedDB in the browser when it isn't:
240240
241241
```js {3}
242-
function useBrowserQuery(query, options) {
243-
if (options.initialData === undefined) {
244-
use(browser('useBrowserQuery: No initial data was provided.'));
242+
function useDraft(draftId, initialDraft) {
243+
if (initialDraft !== undefined) {
244+
return initialDraft;
245245
}
246246

247-
return useQuery(query, options);
248-
}
249-
250-
function ProductDetails({ productId, initialData }) {
251-
const product = useBrowserQuery(`/api/products/${productId}`, {
252-
initialData,
253-
});
254-
255-
return <h1>{product.name}</h1>;
247+
use(browser('The draft is stored in IndexedDB.'));
248+
return use(readDraft(draftId));
256249
}
257250
```
258251
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.
252+
On the server, `useDraft` returns `initialDraft` when it is provided. Otherwise, the closest Suspense boundary's fallback remains in the HTML. In the browser, `use(browser())` returns `undefined`, so the Hook continues and reads the draft from IndexedDB.
260253
261-
This example renders one query with initial data and one without it.
254+
This example renders one draft with initial data and one stored in IndexedDB.
262255
263-
Click **Reload** to see the second product's loading fallback before its query resolves.
256+
Click **Reload** to see the second draft's loading fallback while React reads it from IndexedDB.
264257
265258
<Sandpack>
266259
267260
```js src/App.js active
268261
import { Suspense } from 'react';
269-
import { useBrowserQuery } from './useBrowserQuery.js';
262+
import { useDraft } from './useDraft.js';
270263

271-
function ProductDetails({productId, initialData}) {
272-
const product = useBrowserQuery(`/api/products/${productId}`, {
273-
initialData,
274-
});
275-
return <strong>{product.name}</strong>;
264+
function Draft({draftId, title, initialDraft}) {
265+
const draft = useDraft(draftId, initialDraft);
266+
return (
267+
<li>
268+
<strong>{title}</strong>
269+
<p>{draft}</p>
270+
</li>
271+
);
276272
}
277273

278274
export default function App() {
279275
return (
280276
<>
281-
<h1>Featured products</h1>
277+
<h1>Saved drafts</h1>
282278
<ul>
283-
<li>
284-
<ProductDetails
285-
productId="react-mug"
286-
initialData={{name: 'React mug'}}
287-
/>
288-
</li>
289-
<Suspense fallback={<li>Loading another product...</li>}>
290-
<li>
291-
<ProductDetails productId="react-shirt" />
292-
</li>
279+
<Draft
280+
draftId="release-notes"
281+
title="Release notes"
282+
initialDraft="Announce the next release."
283+
/>
284+
<Suspense fallback={<li>Loading saved draft...</li>}>
285+
<Draft draftId="trip-notes" title="Trip notes" />
293286
</Suspense>
294287
</ul>
295288
</>
296289
);
297290
}
298291
```
299292
300-
```js src/useBrowserQuery.js
293+
```js src/useDraft.js
301294
import { use } from 'react';
302295
import { browser } from 'react-dom';
303-
import { useQuery } from './query.js';
296+
import { readDraft } from './database.js';
304297

305-
export function useBrowserQuery(query, options) {
306-
if (options.initialData === undefined) {
307-
use(browser('useBrowserQuery: No initial data was provided.'));
298+
export function useDraft(draftId, initialDraft) {
299+
if (initialDraft !== undefined) {
300+
return initialDraft;
308301
}
309-
return useQuery(query, options);
302+
303+
use(browser('The draft is stored in IndexedDB.'));
304+
return use(readDraft(draftId));
310305
}
311306
```
312307
313-
```js src/query.js hidden
314-
import { use } from 'react';
315-
316-
// This is a simplified implementation of a
317-
// Suspense-enabled query library.
318-
319-
const products = {
320-
'/api/products/react-shirt': {name: 'React shirt'},
308+
```js src/database.js hidden
309+
const drafts = {
310+
'trip-notes': 'Remember to pack a charger.',
321311
};
322312

323313
const cache = new Map();
324314

325-
function fetchProduct(query) {
326-
if (!cache.has(query)) {
327-
cache.set(
328-
query,
329-
new Promise(resolve => {
330-
setTimeout(() => resolve(products[query]), 600);
331-
})
332-
);
315+
export function readDraft(draftId) {
316+
if (!cache.has(draftId)) {
317+
cache.set(draftId, readDraftFromIndexedDB(draftId));
333318
}
334-
return cache.get(query);
319+
return cache.get(draftId);
335320
}
336321

337-
export function useQuery(query, options) {
338-
if (options.initialData !== undefined) {
339-
return options.initialData;
340-
}
341-
return use(fetchProduct(query));
322+
function readDraftFromIndexedDB(draftId) {
323+
return new Promise((resolve, reject) => {
324+
const request = indexedDB.open('browser-example', 1);
325+
326+
request.onupgradeneeded = () => {
327+
const store = request.result.createObjectStore('drafts');
328+
for (const [key, value] of Object.entries(drafts)) {
329+
store.add(value, key);
330+
}
331+
};
332+
333+
request.onerror = () => reject(request.error);
334+
request.onsuccess = () => {
335+
const transaction = request.result.transaction('drafts');
336+
const draftRequest = transaction.objectStore('drafts').get(draftId);
337+
draftRequest.onerror = () => reject(draftRequest.error);
338+
draftRequest.onsuccess = () => {
339+
setTimeout(() => resolve(draftRequest.result), 600);
340+
};
341+
};
342+
});
342343
}
343344
```
344345
@@ -349,7 +350,7 @@ export default function Document() {
349350
return (
350351
<html lang="en">
351352
<head>
352-
<title>Featured products</title>
353+
<title>Saved drafts</title>
353354
<style>{`
354355
h1 { font-size: 24px; margin-top: 0; }
355356
`}</style>

0 commit comments

Comments
 (0)