diff --git a/.changeset/friendly-flowers-run.md b/.changeset/friendly-flowers-run.md new file mode 100644 index 00000000000..27de764af96 --- /dev/null +++ b/.changeset/friendly-flowers-run.md @@ -0,0 +1,5 @@ +--- +'@builder.io/qwik': patch +--- + +docs: fix useResource docs example & remove unused demo diff --git a/packages/docs/src/routes/demo/state/resource/index.tsx b/packages/docs/src/routes/demo/state/resource/index.tsx index dd666078abd..121365328b1 100644 --- a/packages/docs/src/routes/demo/state/resource/index.tsx +++ b/packages/docs/src/routes/demo/state/resource/index.tsx @@ -6,25 +6,35 @@ import { } from '@builder.io/qwik'; export default component$(() => { - const prNumber = useSignal('3576'); + const postId = useSignal('23'); - const prTitle = useResource$(async ({ track }) => { - // it will run first on mount (server), then re-run whenever prNumber changes (client) + const postTitle = useResource$(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 `` component + return `invalid post '${postId.value}'`; + } }); return ( <> - -

PR#{prNumber}:

+ +

Post#{postId}:

Loading...

} onResolved={(title) =>

{title}

} /> diff --git a/packages/docs/src/routes/demo/tasks/resource/index.tsx b/packages/docs/src/routes/demo/tasks/resource/index.tsx deleted file mode 100644 index c7920fd9bf2..00000000000 --- a/packages/docs/src/routes/demo/tasks/resource/index.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import { - component$, - Resource, - useResource$, - useSignal, -} from '@builder.io/qwik'; - -export default component$(() => { - const prNumber = useSignal('3576'); - - const prTitle = useResource$(async ({ track }) => { - track(() => prNumber.value); // Requires explicit tracking of inputs - const response = await fetch( - `https://api.github.com/repos/QwikDev/qwik/pulls/${prNumber.value}` - ); - const data = await response.json(); - return (data.title || data.message || 'Error') as string; - }); - - return ( - <> - -

- PR#{prNumber}: - <>Loading...} - onResolved={(title) => <>{title}} - /> -

- - ); -}); diff --git a/packages/docs/src/routes/docs/(qwik)/components/state/index.mdx b/packages/docs/src/routes/docs/(qwik)/components/state/index.mdx index 1ac45e29058..93137a97b99 100644 --- a/packages/docs/src/routes/docs/(qwik)/components/state/index.mdx +++ b/packages/docs/src/routes/docs/(qwik)/components/state/index.mdx @@ -35,6 +35,7 @@ contributors: - Balastrong - Jemsco - shairez + - ianlet updated_at: '2023-10-04T21:48:45Z' created_at: '2023-03-20T23:45:13Z' --- @@ -277,14 +278,15 @@ import { } from '@builder.io/qwik'; export default component$(() => { - const prNumber = useSignal('3576'); + const postId = useSignal('23'); - const prTitle = useResource$(async ({ track }) => { - // it will run first on mount (server), then re-run whenever prNumber changes (client) + const postTitle = useResource$(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; @@ -292,10 +294,10 @@ export default component$(() => { return ( <> - -

PR#{prNumber}:

+ +

Post#{postId}:

Loading...

} onResolved={(title) =>

{title}

} />