Skip to content

Commit bc3daab

Browse files
committed
Doc updates
1 parent 343ca20 commit bc3daab

File tree

3 files changed

+156
-2
lines changed

3 files changed

+156
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
node_modules
22
dist
3+
app-js
4+
app-ts
5+
app-js-tw
6+
app-ts-tw

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ pnpm i
1616
| `npx . app-ts --template typescript` | Creates a TypeScript app |
1717
| `npx . app-js-tw --tailwind` | Creates a JavaScript app with Tailwind CSS |
1818
| `npx . app-ts-tw --template typescript --tailwind` | Creates a TypeScript app with Tailwind CSS |
19+
20+
You can also add `--tailwind` to the command to automatically add Tailwind CSS to the app.

project-template/README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Then anywhere in your JSX you can use it like so:
6767

6868
This will create a link that will navigate to the `/about` route.
6969

70+
More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
71+
7072
### Using A Layout
7173

7274
Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
@@ -105,6 +107,146 @@ const rootRoute = createRootRoute({
105107

106108
The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
107109

110+
More information on layouts can be found in the [Layouts documentation](hthttps://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
111+
112+
### Migrating To File Base Routing
113+
114+
First you need to add the Vite plugin for Tanstack Router:
115+
116+
```bash
117+
npm install -D @tanstack/router-plugin
118+
```
119+
120+
From there you need to update your `vite.config.ts` file to use the plugin:
121+
122+
```ts
123+
// vite.config.ts
124+
import { defineConfig } from "vite";
125+
import viteReact from "@vitejs/plugin-react";
126+
import { TanStackRouterVite } from "@tanstack/router-plugin/vite";
127+
128+
// https://vitejs.dev/config/
129+
export default defineConfig({
130+
plugins: [
131+
TanStackRouterVite(),
132+
viteReact(),
133+
// ...
134+
],
135+
});
136+
```
137+
138+
Now you'll need to rearrange your files a little bit. That starts with creating a `routes` directory in the `src` directory:
139+
140+
```bash
141+
mkdir src/routes
142+
```
143+
144+
Then you'll need to create a `src/routes/__root.tsx` file with the contents of the root route that was in `main.tsx`.
145+
146+
```tsx
147+
import { createRootRoute, Outlet } from "@tanstack/react-router";
148+
import { TanStackRouterDevtools } from "@tanstack/router-devtools";
149+
150+
export const Route = createRootRoute({
151+
component: () => (
152+
<>
153+
<Outlet />
154+
<TanStackRouterDevtools />
155+
</>
156+
),
157+
});
158+
```
159+
160+
Next up you'll need to move your home route code into `src/routes/index.tsx`
161+
162+
```tsx
163+
import { createFileRoute } from "@tanstack/react-router";
164+
165+
import logo from "../logo.svg";
166+
import "../App.css";
167+
168+
export const Route = createFileRoute("/")({
169+
component: App,
170+
});
171+
172+
function App() {
173+
return (
174+
<div className="App">
175+
<header className="App-header">
176+
<img src={logo} className="App-logo" alt="logo" />
177+
<p>
178+
Edit <code>src/App.tsx</code> and save to reload.
179+
</p>
180+
<a
181+
className="App-link"
182+
href="https://reactjs.org"
183+
target="_blank"
184+
rel="noopener noreferrer"
185+
>
186+
Learn React
187+
</a>
188+
<a
189+
className="App-link"
190+
href="https://tanstack.com"
191+
target="_blank"
192+
rel="noopener noreferrer"
193+
>
194+
Learn TanStack
195+
</a>
196+
</header>
197+
</div>
198+
);
199+
}
200+
```
201+
202+
At this point you can delete `src/App.tsx`, you will no longer need it as the contents have moved into `src/routes/index.tsx`.
203+
204+
The only additional code is the `createFileRoute` function that tells TanStack Router where to render the route. Helpfully the Vite plugin will keep the path argument that goes to `createFileRoute` automatically in sync with the file system.
205+
206+
Finally the `src/main.tsx` file can be simplified down to this:
207+
208+
```tsx
209+
import { StrictMode } from "react";
210+
import ReactDOM from "react-dom/client";
211+
import { RouterProvider, createRouter } from "@tanstack/react-router";
212+
213+
// Import the generated route tree
214+
import { routeTree } from "./routeTree.gen";
215+
216+
import "./styles.css";
217+
import reportWebVitals from "./reportWebVitals";
218+
219+
// Create a new router instance
220+
const router = createRouter({ routeTree });
221+
222+
// Register the router instance for type safety
223+
declare module "@tanstack/react-router" {
224+
interface Register {
225+
router: typeof router;
226+
}
227+
}
228+
229+
// Render the app
230+
const rootElement = document.getElementById("app")!;
231+
if (!rootElement.innerHTML) {
232+
const root = ReactDOM.createRoot(rootElement);
233+
root.render(
234+
<StrictMode>
235+
<RouterProvider router={router} />
236+
</StrictMode>
237+
);
238+
}
239+
240+
// If you want to start measuring performance in your app, pass a function
241+
// to log results (for example: reportWebVitals(console.log))
242+
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
243+
reportWebVitals();
244+
```
245+
246+
Now you've got a file based routing setup in your project! Let's have some fun with it! Just create a file in `about.tsx` in `src/routes` and it if the application is running TanStack will automatically add contents to the file and you'll have the start of your `/about` route ready to go with no additional work. You can see why folks find File Based Routing so easy to use.
247+
248+
You can find out everything you need to know on how to use file based routing in the [File Based Routing](https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing) documentation.
249+
108250
## Data Fetching
109251

110252
There are multiple ways to fetch data in your application. You can use TanStack Query to fetch data from a server. But you can also use the `loader` functionality built into TanStack Router to load the data for a route before it's rendered.
@@ -117,10 +259,14 @@ const peopleRoute = createRoute({
117259
path: "/people",
118260
loader: async () => {
119261
const response = await fetch("https://swapi.dev/api/people");
120-
return response.json();
262+
return response.json() as Promise<{
263+
results: {
264+
name: string;
265+
}[];
266+
}>;
121267
},
122268
component: () => {
123-
const data = useLoaderData<typeof loader>();
269+
const data = peopleRoute.useLoaderData();
124270
return (
125271
<ul>
126272
{data.results.map((person) => (
@@ -131,3 +277,5 @@ const peopleRoute = createRoute({
131277
},
132278
});
133279
```
280+
281+
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).

0 commit comments

Comments
 (0)