agentleFS
charanjit-singhcharanjit-singh/lovable-skills/skills/tanstack-infinite-queries/SKILL.md

tanstack-infinite-queries

Skill10 starsChanged 6 months agoMITView on GitHub

Use when implementing pagination, infinite scroll, or "load more" lists with TanStack Query: useInfiniteQuery, cursor-based or offset-based pagination, virtualization. Not for single-page lists where one query is enough.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use useInfiniteQuery for any list that can grow beyond ~50 rows.

    line 11
  2. Distinctive:

    After complex changes, invalidate to reconcile with the server:

    line 120

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

TanStack infinite queries (pagination / infinite scroll)

Use useInfiniteQuery for any list that can grow beyond ~50 rows. Pair with cursor-based pagination (Supabase lt(created_at, lastCursor)) — offset pagination breaks when rows are inserted.

Companion skills: tanstack-query-alternative, postgres-full-text-search, error-states-and-empty-ui.

Cursor-based fetcher (Supabase)

// src/api/posts.ts
const PAGE_SIZE = 20;

export async function fetchPostsPage({
  orgId,
  cursor,
}: { orgId: string; cursor?: string }) {
  let q = supabase
    .from("posts")
    .select("*")
    .eq("organization_id", orgId)
    .is("deleted_at", null)
    .order("created_at", { ascending: false })
    .order("id", { ascending: false })  // tiebreaker, stable order
    .limit(PAGE_SIZE);

  if (cursor) q = q.lt("created_at", cursor);

  const { data, error } = await q;
  if (error) throw error;

  const nextCursor = data.length === PAGE_SIZE ? data[data.length - 1].created_at : undefined;
  return { rows: data, nextCursor };
}

order("id") as a tiebreaker prevents skipped rows when multiple records share a created_at.

Hook

// src/hooks/queries/usePosts.ts
import { useInfiniteQuery } from "@tanstack/react-query";

export function usePosts(orgId: string) {
  return useInfiniteQuery({
    queryKey: postKeys.list({ orgId }),
    queryFn: ({ pageParam }) => fetchPostsPage({ orgId, cursor: pageParam }),
    initialPageParam: undefined as string | undefined,
    getNextPageParam: (last) => last.nextCursor,
    staleTime: 30_000,
  });
}

Flatten in the component:

const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = usePosts(orgId);
const posts = data?.pages.flatMap((p) => p.rows) ?? [];

Trigger next page

Intersection observer (recommended)

const ref = useIntersectionObserver(() => {
  if (hasNextPage && !isFetchingNextPage) fetchNextPage();
});

return (
  <>
    {posts.map((p) => <PostCard key={p.id} post={p} />)}
    <div ref={ref} />
    {isFetchingNextPage && <Skeleton />}
  </>
);

Button fallback

A "Load more" button is more accessible than infinite scroll for long lists. Combine: button + observer triggers below it.

Virtualization (large lists)

For ≥ 200 rendered rows, add @tanstack/react-virtual so the DOM only holds visible items:

const rowVirtualizer = useVirtualizer({
  count: posts.length,
  getScrollElement: () => parentRef.current,
  estimateSize: () => 88,
  overscan: 8,
});

Required for chat, activity feeds, and ledger views — without it, scroll jank kills UX.

Inserts and deletes — cache updates

// after creating a post, prepend to the first page
qc.setQueryData<InfiniteData<typeof first>>(postKeys.list({ orgId }), (old) => {
  if (!old) return old;
  const [first, ...rest] = old.pages;
  return { ...old, pages: [{ ...first, rows: [newPost, ...first.rows] }, ...rest] };
});

For deletes, filter rows across all pages.

After complex changes, invalidate to reconcile with the server:

qc.invalidateQueries({ queryKey: postKeys.list({ orgId }) });

Avoid

  • Offset pagination (range(0, 19) / range(20, 39)) on tables with frequent inserts — rows shift.
  • Missing tiebreaker order — duplicate or missing rows at page boundaries.
  • Rendering 1000 rows without virtualization.
  • Loading the next page on a 1 px scroll trigger (causes thrash) — use observer with margin.
  • Re-fetching every page on a single delete; surgical cache update instead.

Checklist

  • Cursor-based query with stable tiebreaker.
  • hasNextPage + isFetchingNextPage UX.
  • Virtualization on long lists.
  • Surgical setQueryData on insert/delete; invalidate to reconcile.
  • Empty / error states (error-states-and-empty-ui).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.