Skip to content

Commit f9e7f25

Browse files
committed
RQ added to the project docs
1 parent 1663538 commit f9e7f25

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

project-template/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,84 @@ const peopleRoute = createRoute({
279279
```
280280

281281
Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#loader-parameters).
282+
283+
### React-Query
284+
285+
React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
286+
287+
First add your dependencies:
288+
289+
```bash
290+
npm install @tanstack/react-query @tanstack/react-query-devtools
291+
```
292+
293+
Next we'll need to creata query client and provider. We recommend putting those in `main.tsx`.
294+
295+
```tsx
296+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
297+
298+
// ...
299+
300+
const queryClient = new QueryClient();
301+
302+
// ...
303+
304+
if (!rootElement.innerHTML) {
305+
const root = ReactDOM.createRoot(rootElement);
306+
307+
root.render(
308+
<QueryClientProvider client={queryClient}>
309+
<RouterProvider router={router} />
310+
</QueryClientProvider>
311+
);
312+
}
313+
```
314+
315+
You can also add TanStack Query Devtools to the root route (optional).
316+
317+
```tsx
318+
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
319+
320+
const rootRoute = createRootRoute({
321+
component: () => (
322+
<>
323+
<Outlet />
324+
<ReactQueryDevtools buttonPosition="top-right" />
325+
<TanStackRouterDevtools />
326+
</>
327+
),
328+
});
329+
```
330+
331+
Now you can use `useQuery` to fetch your data.
332+
333+
```tsx
334+
import { useQuery } from "@tanstack/react-query";
335+
336+
import "./App.css";
337+
338+
function App() {
339+
const { data } = useQuery({
340+
queryKey: ["people"],
341+
queryFn: () =>
342+
fetch("https://swapi.dev/api/people")
343+
.then((res) => res.json())
344+
.then((data) => data.results as { name: string }[]),
345+
initialData: [],
346+
});
347+
348+
return (
349+
<div>
350+
<ul>
351+
{data.map((person) => (
352+
<li key={person.name}>{person.name}</li>
353+
))}
354+
</ul>
355+
</div>
356+
);
357+
}
358+
359+
export default App;
360+
```
361+
362+
You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).

0 commit comments

Comments
 (0)