Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix useResource example & remove unused demo #6893

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/friendly-flowers-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik': patch
---

docs: fix useResource docs example & remove unused demo
34 changes: 22 additions & 12 deletions packages/docs/src/routes/demo/state/resource/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@ import {
} from '@builder.io/qwik';

export default component$(() => {
const prNumber = useSignal('3576');
const postId = useSignal('23');

const prTitle = useResource$<string>(async ({ track }) => {
// it will run first on mount (server), then re-run whenever prNumber changes (client)
const postTitle = useResource$<string>(async ({ track, cleanup }) => {
// It will run first on mount (server), then re-run whenever postId changes (client)
// this means this code will run on the server and the browser
track(() => prNumber.value);
const response = await fetch(
`https://api.github.com/repos/QwikDev/qwik/pulls/${prNumber.value}`
);
const data = await response.json();
return data.title as string;
const controller = new AbortController();
track(() => postId.value);
cleanup(() => controller.abort());

try {
const response = await fetch(
`https://jsonplaceholder.typicode.com/posts/${postId.value}`,
{ signal: controller.signal }
);
const data = await response.json();
return data.title as string;
} catch (e) {
// For demo purposes only, we recommend not to use try/catch inside useResource$
// and instead use the `onRejected` handler on the `<Resource />` component
return `invalid post '${postId.value}'`;
}
});

return (
<>
<input type="number" bind:value={prNumber} />
<h1>PR#{prNumber}:</h1>
<input type="number" bind:value={postId} max={100} min={0} />
<h1>Post#{postId}:</h1>
<Resource
value={prTitle}
value={postTitle}
onPending={() => <p>Loading...</p>}
onResolved={(title) => <h2>{title}</h2>}
/>
Expand Down
33 changes: 0 additions & 33 deletions packages/docs/src/routes/demo/tasks/resource/index.tsx

This file was deleted.

18 changes: 10 additions & 8 deletions packages/docs/src/routes/docs/(qwik)/components/state/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contributors:
- Balastrong
- Jemsco
- shairez
- ianlet
updated_at: '2023-10-04T21:48:45Z'
created_at: '2023-03-20T23:45:13Z'
---
Expand Down Expand Up @@ -277,25 +278,26 @@ import {
} from '@builder.io/qwik';

export default component$(() => {
const prNumber = useSignal('3576');
const postId = useSignal('23');

const prTitle = useResource$<string>(async ({ track }) => {
// it will run first on mount (server), then re-run whenever prNumber changes (client)
const postTitle = useResource$<string>(async ({ track }) => {
// it will run first on mount (server), then re-run whenever postId changes (client)
// this means this code will run on the server and the browser
track(() => prNumber.value);
track(() => postId.value);

const response = await fetch(
`https://api.github.com/repos/QwikDev/qwik/pulls/${prNumber.value}`
`https://jsonplaceholder.typicode.com/posts/${postId.value}`
);
const data = await response.json();
return data.title as string;
});

return (
<>
<input type="number" bind:value={prNumber} />
<h1>PR#{prNumber}:</h1>
<input type="number" bind:value={postId} max={100} min={0} />
<h1>Post#{postId}:</h1>
<Resource
value={prTitle}
value={postTitle}
onPending={() => <p>Loading...</p>}
onResolved={(title) => <h2>{title}</h2>}
/>
Expand Down