React + TanStack Query
Server state: caching, refetching, loading and error states. This page shows exactly what pickstack adds to a React project for TanStack Query: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework react --modules tanstack-query --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@tanstack/react-query | ^5.108.4 | runtime |
New files
["React 19", "Vite", "TypeScript", "TanStack Query"]
/** Loads the stack list served from `public/stack.json`. */
export async function fetchStack(): Promise<string[]> {
const response = await fetch(`${import.meta.env.BASE_URL}stack.json`);
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
return response.json();
}
import { useQuery } from "@tanstack/react-query";
import { fetchStack } from "../api/stack";
export default function StackList() {
const stack = useQuery({ queryKey: ["stack"], queryFn: fetchStack });
return (
<p className="text">
{stack.isPending
? "Loading…"
: stack.isError
? "Could not load the stack."
: `Built with ${stack.data.join(", ")}.`}
</p>
);
}
Changes to existing files
import Counter from "./components/Counter";+import StackList from "./components/StackList"; export default function App() { return ( <main className="app"> <h1 className="title">my-app</h1> <Counter />+ <StackList /> </main> ); }⋯ 1 unchanged lines
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import App from "./App"; import "./index.css"; +const queryClient = new QueryClient();+ createRoot(document.getElementById("root")!).render( <StrictMode>+ <QueryClientProvider client={queryClient}>+ <App />+ </QueryClientProvider>− <App /> </StrictMode>, );
Project files
.gitignoreREADME.mdindex.htmlpackage.jsonpublic/stack.jsonsrc/App.tsxsrc/api/stack.tssrc/components/Counter.tsxsrc/components/StackList.tsxsrc/index.csssrc/main.tsxtsconfig.jsonvite.config.ts