Skip to content

Commit 987d6b9

Browse files
committed
docs: fix useResource example
* Replace github API call with a free fake & reliable API * Wrap API call in try/catch to prevent future error 500
1 parent b219343 commit 987d6b9

File tree

4 files changed

+37
-53
lines changed

4 files changed

+37
-53
lines changed

.changeset/friendly-flowers-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik': patch
3+
---
4+
5+
docs: fix useResource docs example & remove unused demo

packages/docs/src/routes/demo/state/resource/index.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,35 @@ import {
66
} from '@builder.io/qwik';
77

88
export default component$(() => {
9-
const prNumber = useSignal('3576');
9+
const postId = useSignal('23');
1010

11-
const prTitle = useResource$<string>(async ({ track }) => {
12-
// it will run first on mount (server), then re-run whenever prNumber changes (client)
11+
const postTitle = useResource$<string>(async ({ track, cleanup }) => {
12+
// It will run first on mount (server), then re-run whenever postId changes (client)
1313
// this means this code will run on the server and the browser
14-
track(() => prNumber.value);
15-
const response = await fetch(
16-
`https://api.github.com/repos/QwikDev/qwik/pulls/${prNumber.value}`
17-
);
18-
const data = await response.json();
19-
return data.title as string;
14+
const controller = new AbortController();
15+
track(() => postId.value);
16+
cleanup(() => controller.abort());
17+
18+
try {
19+
const response = await fetch(
20+
`https://jsonplaceholder.typicode.com/posts/${postId.value}`,
21+
{ signal: controller.signal }
22+
);
23+
const data = await response.json();
24+
return data.title as string;
25+
} catch (e) {
26+
// For demo purposes only, we recommend not to use try/catch inside useResource$
27+
// and instead use the `onRejected` handler on the `<Resource />` component
28+
return `invalid post '${postId.value}'`;
29+
}
2030
});
2131

2232
return (
2333
<>
24-
<input type="number" bind:value={prNumber} />
25-
<h1>PR#{prNumber}:</h1>
34+
<input type="number" bind:value={postId} max={100} min={0} />
35+
<h1>Post#{postId}:</h1>
2636
<Resource
27-
value={prTitle}
37+
value={postTitle}
2838
onPending={() => <p>Loading...</p>}
2939
onResolved={(title) => <h2>{title}</h2>}
3040
/>

packages/docs/src/routes/demo/tasks/resource/index.tsx

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/docs/src/routes/docs/(qwik)/components/state/index.mdx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ contributors:
3535
- Balastrong
3636
- Jemsco
3737
- shairez
38+
- ianlet
3839
updated_at: '2023-10-04T21:48:45Z'
3940
created_at: '2023-03-20T23:45:13Z'
4041
---
@@ -277,25 +278,26 @@ import {
277278
} from '@builder.io/qwik';
278279

279280
export default component$(() => {
280-
const prNumber = useSignal('3576');
281+
const postId = useSignal('23');
281282

282-
const prTitle = useResource$<string>(async ({ track }) => {
283-
// it will run first on mount (server), then re-run whenever prNumber changes (client)
283+
const postTitle = useResource$<string>(async ({ track }) => {
284+
// it will run first on mount (server), then re-run whenever postId changes (client)
284285
// this means this code will run on the server and the browser
285-
track(() => prNumber.value);
286+
track(() => postId.value);
287+
286288
const response = await fetch(
287-
`https://api.github.com/repos/QwikDev/qwik/pulls/${prNumber.value}`
289+
`https://jsonplaceholder.typicode.com/posts/${postId.value}`
288290
);
289291
const data = await response.json();
290292
return data.title as string;
291293
});
292294

293295
return (
294296
<>
295-
<input type="number" bind:value={prNumber} />
296-
<h1>PR#{prNumber}:</h1>
297+
<input type="number" bind:value={postId} max={100} min={0} />
298+
<h1>Post#{postId}:</h1>
297299
<Resource
298-
value={prTitle}
300+
value={postTitle}
299301
onPending={() => <p>Loading...</p>}
300302
onResolved={(title) => <h2>{title}</h2>}
301303
/>

0 commit comments

Comments
 (0)