Skip to content

Fix Qwik Component Resource – Resource Not Resolving

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Fix Qwik Component Resource. We cover key concepts, practical examples, and best practices.

You use useResource$ in a Qwik component to fetch data asynchronously. The component renders, but resource.value is always undefined — or the resource stays in a pending state forever.

Wrong ❌

import { component$, useResource$ } from '@builder.io/qwik';

export default component$(() => {
  const resource = useResource$<string[]>(async () => {
    // ❌ Tracked function doesn't read any signals
    const res = await fetch('/api/items');
    return res.json();
  });

  return (
    <div>
      {resource.value?.length ?? 'Loading...'} items
    </div>
  );
});

The resource never resolves. The page shows `Loading...` indefinitely.

## Right ✅

```tsx
import { component$, useResource$, useSignal, Resource } from '@<a href="/design-patterns/builder/">builder</a>.io/qwik';

export default component$(() => {
  const page = useSignal(1);
  const search = useSignal('');

  const resource = useResource$<string[]>(async ({ track, cleanup }) => {
    // ✅ Track signals — resource re-runs when they change
    track(() => page.value);
    track(() => search.value);

    // ✅ Cleanup — abort in-flight requests
    const controller = new AbortController();
    cleanup(() => controller.abort());

    const res = await fetch(`/api/items?page=${page.value}&q=${search.value}`, {
      signal: controller.signal,
    });
    return res.json();
  });

  return (
    <div>
      <input onInput$={(_, el) => (search.value = el.value)} placeholder="Search" />
      <button onClick$={() => page.value++}>Next page</button>

      {/* ✅ Use <Resource> component for loading/error states */}
      <Resource
        value={resource}
        onPending={() => <div>Loading...</div>}
        onRejected={(err) => <div>Error: {err.message}</div>}
        onResolved={(items) => (
          <ul>
            {items.map((item) => <li>{item}</li>)}
          </ul>
        )}
      />
    </div>
  );
});

Now the resource resolves on first render and re‑fetches when `page` or `search` changes:

✅ Resource resolved: [ "item1", "item2", "item3" ]

**Common mistake — not using the `<Resource>` component:**

```tsx
// ❌ Manual conditional — works but verbose
{resource.value ? <List items={resource.value} /> : <Loading />}

// ✅ Cleaner with <Resource>
<Resource
  value={resource}
  onPending={() => <Skeleton />}
  onRejected={(err) => <ErrorFallback err={err} />}
  onResolved={(data) => <List items={data} />}
/>

**Debugging:**

```tsx
const resource = useResource$<string[]>(async ({ track, cleanup }) => {
  console.log('Resource function started');
  track(() => page.value);
  // ... fetch
  console.log('Resource resolved');
  return data;
});

## Root Cause

`useResource$` runs its async function lazily and tracks dependencies via `track()`. If no signals are tracked, the resource runs once (on mount) but may appear to hang if the fetch fails or the component unmounts/cancels.

## Prevention

- Always call `track(() => signal)` for every signal the resource depends on.
- Use the `<Resource>` component for declarative loading/error states.
- Implement cleanup with `cleanup()` to abort fetch requests.
- Use `useResource$` for client‑side data that updates reactively — use `routeLoader$` for server‑side data.


## Common Mistakes with component resource

1. **Misunderstanding that `String` is `[Char]` with poor performance for large text operations**
2. **Using `foldl` instead of `foldl'` causing stack overflow on large lists**
3. **Forgetting `deriving (Show, Eq)` on custom data types needed for debugging**

These mistakes appear frequently in real-world QWIK code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

## Practice Exercise

**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What's the difference between `useResource$` and `routeLoader$`?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: <code>routeLoader$</code> runs on the server during SSR. <code>useResource$</code> runs on the client (can also run during SSR).</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I use `useResource$` without tracking?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — it runs once on mount. But it won't re‑run when signals change.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I refetch a resource manually?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Change a tracked signal's value. Pushing a new value to the tracked signal triggers re‑execution.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What happens if the component unmounts before the resource resolves?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: The cleanup function runs (if provided), and the result is discarded.</p>
</div></details>

---

*Resources are covered in the [DodaTech Qwik Data Fetching course](https://dodatech.com/courses/qwik).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro