|
| 1 | +# 02 Fetching |
| 2 | + |
| 3 | +In this example we are going to add data fetching capabilities using TanStack Router. |
| 4 | + |
| 5 | +We will start from `01-routing`. |
| 6 | + |
| 7 | +# Steps to build it |
| 8 | + |
| 9 | +`npm install` to install previous sample packages: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install |
| 13 | +``` |
| 14 | + |
| 15 | +Similar to `getServerSideProps` in Nextjs or `loaders` in Remix/React Router, TanStack Router provides a way to fetch data before rendering the route using `loader` functions and also provides a built-in SWR Caching mechanism. |
| 16 | + |
| 17 | +> [More info about Loaders](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading) |
| 18 | +
|
| 19 | +Let's create a new route with data fetching capabilities, for that we will update our `cars` route to include a `loader` function that fetches car data from a mock API: |
| 20 | + |
| 21 | +_./src/routes/cars/index.tsx_ |
| 22 | + |
| 23 | +```diff |
| 24 | +import { createFileRoute, useNavigate, Link } from '@tanstack/react-router'; |
| 25 | + |
| 26 | ++ const getCarList = async () => |
| 27 | ++ await fetch('/api/cars').then((res) => res.json()); |
| 28 | + |
| 29 | +export const Route = createFileRoute('/cars/')({ |
| 30 | ++ loader: () => getCarList(), |
| 31 | + component: RouteComponent, |
| 32 | +}); |
| 33 | + |
| 34 | +function RouteComponent() { |
| 35 | + const navigate = useNavigate(); |
| 36 | ++ const cars = Route.useLoaderData(); |
| 37 | + |
| 38 | + return ( |
| 39 | + <> |
| 40 | +- <div>Hello "/cars"!</div> |
| 41 | +- <Link to="/cars/$id" params={{ id: '1' }}> |
| 42 | +- Navigate to car 1 |
| 43 | +- </Link> |
| 44 | ++ <ul> |
| 45 | ++ {cars.map((car) => ( |
| 46 | ++ <li key={car.id}> |
| 47 | ++ <Link to="/cars/$id" params={{ id: car.id }}> |
| 48 | ++ {car.name} |
| 49 | ++ </Link> |
| 50 | ++ </li> |
| 51 | ++ ))} |
| 52 | ++ </ul> |
| 53 | + <button onClick={() => navigate({ to: '/' })}>Go back to home</button> |
| 54 | + </> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +> You can play with the `staleTime` props on the route, or even in the global router config to adjust caching behavior. |
| 61 | +
|
| 62 | +But also we need to update the `vite.config.ts` to add the proxy for our mock API to avoid CORS issues: |
| 63 | + |
| 64 | +_./vite.config.ts_ |
| 65 | + |
| 66 | +```diff |
| 67 | +import { tanstackRouter } from '@tanstack/router-plugin/vite'; |
| 68 | +import react from '@vitejs/plugin-react'; |
| 69 | +import { defineConfig } from 'vite'; |
| 70 | + |
| 71 | +export default defineConfig({ |
| 72 | + plugins: [ |
| 73 | + tanstackRouter({ |
| 74 | + target: 'react', |
| 75 | + autoCodeSplitting: true, |
| 76 | + }), |
| 77 | + react(), |
| 78 | + ], |
| 79 | + css: { |
| 80 | + modules: { |
| 81 | + localsConvention: 'camelCase', |
| 82 | + }, |
| 83 | + }, |
| 84 | ++ server: { |
| 85 | ++ proxy: { |
| 86 | ++ '/api': 'http://localhost:3001', |
| 87 | ++ }, |
| 88 | ++ }, |
| 89 | +}); |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +While Router has a simple built-in cache mechanism, for more advanced use-cases you can integrate [TanStack Query or similar libraries](https://tanstack.com/router/latest/docs/framework/react/guide/external-data-loading) to handle data fetching and caching. |
| 94 | + |
| 95 | +# About Basefactor + Lemoncode |
| 96 | + |
| 97 | +We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. |
| 98 | + |
| 99 | +[Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. |
| 100 | + |
| 101 | +[Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. |
| 102 | + |
| 103 | +For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend |
0 commit comments