Skip to content

Commit d97f4c3

Browse files
committed
update 02-fetching
1 parent 50633ca commit d97f4c3

30 files changed

Lines changed: 619 additions & 0 deletions
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules
2+
dist
3+
coverage
4+
.awcache
5+
test-report.*
6+
junit.xml
7+
*.log
8+
*.orig
9+
.cache
10+
.env
11+
.next
12+
.swc
13+
!.vscode
14+
.tanstack
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"endOfLine": "lf"
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"files.watcherExclude": {
3+
"**/routeTree.gen.ts": true
4+
},
5+
"search.exclude": {
6+
"**/routeTree.gen.ts": true
7+
},
8+
"files.readonlyInclude": {
9+
"**/routeTree.gen.ts": true
10+
}
11+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "car-api",
3+
"version": "1.0.0",
4+
"description": "Car API",
5+
"type": "module",
6+
"scripts": {
7+
"mock-server": "tsx watch src/index.ts"
8+
},
9+
"author": "Lemoncode",
10+
"license": "MIT",
11+
"dependencies": {
12+
"@hono/node-server": "^1.19.7",
13+
"hono": "^4.11.0"
14+
},
15+
"devDependencies": {
16+
"tsx": "^4.21.0"
17+
}
18+
}
17.2 KB
Loading
22.5 KB
Loading
17.1 KB
Loading
22.8 KB
Loading
27.8 KB
Loading

0 commit comments

Comments
 (0)