Hook proposal >>> useResourceValue #236
Closed
omerman
started this conversation in
Proposals For Qwik
Replies: 2 comments 2 replies
-
|
We also made a very similar hook in our project, but with a slightly different approach: import type { QRL, ResourceFn, ResourceOptions } from "@builder.io/qwik";
import { implicit$FirstArg, useResourceQrl, useStore, useTask$ } from "@builder.io/qwik";
interface InlineResource<T> {
value?: T | null;
loading: boolean;
error?: Error;
}
export function useInlineResourceQrl<T>(
qrl: QRL<ResourceFn<T>>,
opts?: ResourceOptions,
): InlineResource<T> {
const store = useStore<InlineResource<T>>({
value: undefined,
error: undefined,
loading: true,
});
const resource = useResourceQrl(qrl, opts);
useTask$(async ({ track }) => {
const loading = track(() => resource.loading);
if (!loading) {
try {
store.value = await resource.value;
} catch (e) {
store.error = e as Error;
}
}
store.loading = loading;
});
return store;
}
export const useInlineResource$ = /* @__PURE__ */ implicit$FirstArg(useInlineResourceQrl);Usage: const UseInlineResource = component$(() => {
const todoId = useSignal(1);
const todo = useInlineResource$(async ({ track, cleanup }) => {
const id = track(todoId);
const controller = new AbortController();
cleanup(() => controller.abort());
const r = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`, {
signal: controller.signal,
});
return await r.json();
});
return (
<div>
<button onClick$={() => todoId.value++}>Next todo</button>
<ul>
<li>Loading: {todo.loading ? "true" : "false"}</li>
<li>Value: {JSON.stringify(todo.value)}</li>
<li>Error: {todo.error?.message}</li>
</ul>
</div>
);
});If that can be useful to anyone. |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
In v2 we have https://qwikdev-build-v2.qwik-8nx.pages.dev/api/qwik/#asynccomputedreadonlysignal asyncComputed now, which exposes .loading and .error As such, this is completed |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is it about?
a hook that accepts useResource return value, listens to when it finishes
and makes the resource handling in a react query like way
data - Signal that holds the actual awaited value
error - Signal that holds the error
loading - re-export of the myResource.loading
What's the motivation for this proposal?
Basically in the code we can use
<Resource ../>to render something out of the resource.Sometimes we would just want to pass the result as a property to another component.
Thats not possible in the Promise state of the resource's value.
Proposed Solution / Feature
What do you propose?
I actually already have a draft version of my own that seems to work well.
Code examples
The above is more convenient(DXwise) than rendering a Resource with a pending/resolved/error rendering versions.
Its much easier to maintain.
Links / References
No response
Beta Was this translation helpful? Give feedback.
All reactions