Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

README.md.ejs

215 lines7.1 KB

1Welcome to your new TanStack app!
2​
3# Getting Started
4​
5To run this application:
6​
7```bash
8<%= packageManager %> install
9<%= getPackageManagerRunScript('start') %>
10```
11​
12# Building For Production
13​
14To build this application for production:
15​
16```bash
17<%= getPackageManagerRunScript('build') %>
18```
19​
20## Styling
21​
22This project uses [Tailwind CSS](https://tailwindcss.com/) for styling.
23​
24<% for(const addon of addOns.filter(addon => addon.readme)) { %>
25<%- addon.readme %>
26<% } %>
27​
28## Routing
29<% if (fileRouter) { %>This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a file based router. Which means that the routes are managed as fiels in `src/routes`.<% } else { %>This project uses [TanStack Router](https://tanstack.com/router). The initial setup is a code based router. Which means that the routes are defined in code (in the `./src/main.<%= jsx %>` file). If you like you can also use a file based routing setup by following the [File Based Routing](https://tanstack.com/router/latest/docs/framework/solid/guide/file-based-routing) guide.<% } %>
30​
31### Adding A Route
32<% if (fileRouter) { %>
33To add a new route to your application just add another a new file in the `./src/routes` directory.
34​
35TanStack will automatically generate the content of the route file for you.
36<% } else { %>
37To add a new route to your application just add another `createRoute` call to the `./src/main.<%= jsx %>` file. The example below adds a new `/about`route to the root route.
38​
39```tsx
40const aboutRoute = createRoute({
41 getParentRoute: () => rootRoute,
42 path: "/about",
43 component: () => <h1>About</h1>,
44});
45```
46​
47You will also need to add the route to the `routeTree` in the `./src/main.<%= jsx %>` file.
48​
49```tsx
50const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]);
51```
52​
53With this set up you should be able to navigate to `/about` and see the about page.
54​
55Of course you don't need to implement the About page in the `main.<%= jsx %>` file. You can create that component in another file and import it into the `main.<%= jsx %>` file, then use it in the `component` property of the `createRoute` call, like so:
56​
57```tsx
58import About from "./components/About.<%= jsx %>";
59​
60const aboutRoute = createRoute({
61 getParentRoute: () => rootRoute,
62 path: "/about",
63 component: About,
64});
65```
66​
67That is how we have the `App` component set up with the home page.
68​
69For more information on the options you have when you are creating code based routes check out the [Code Based Routing](https://tanstack.com/router/latest/docs/framework/solid/guide/code-based-routing) documentation.
70<% } %>
71Now that you have two routes you can use a `Link` component to navigate between them.
72​
73### Adding Links
74​
75To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/solid-router`.
76​
77```tsx
78import { Link } from "@tanstack/solid-router";
79```
80​
81Then anywhere in your JSX you can use it like so:
82​
83```tsx
84<Link to="/about">About</Link>
85```
86​
87This will create a link that will navigate to the `/about` route.
88​
89More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/solid/api/router/linkComponent).
90​
91### Using A Layout
92​
93<% if (codeRouter) { %>
94Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
95​
96There is already a layout in the `src/main.<%= jsx %>` file:
97​
98```tsx
99const rootRoute = createRootRoute({
100 component: () => (
101 <>
102 <Outlet />
103 <TanStackRouterDevtools />
104 </>
105 ),
106});
107```
108​
109You can use the Soliid component specified in the `component` property of the `rootRoute` to wrap the contents of the routes. The `<Outlet />` component is used to render the current route within the body of the layout. For example you could add a header to the layout like so:
110​
111```tsx
112import { Link } from "@tanstack/solid-router";
113​
114const rootRoute = createRootRoute({
115 component: () => (
116 <>
117 <header>
118 <nav>
119 <Link to="/">Home</Link>
120 <Link to="/about">About</Link>
121 </nav>
122 </header>
123 <Outlet />
124 <TanStackRouterDevtools />
125 </>
126 ),
127});
128```
129<% } else { %>In the File Based Routing setup the layout is located in `src/routes/__root.tsx`. Anything you add to the root route will appear in all the routes. The route content will appear in the JSX where you use the `<Outlet />` component.
130​
131Here is an example layout that includes a header:
132​
133```tsx
134import { Outlet, createRootRoute } from '@tanstack/solid-router'
135import { TanStackRouterDevtools } from '@tanstack/solid-router-devtools'
136​
137import { Link } from "@tanstack/solid-router";
138​
139export const Route = createRootRoute({
140 component: () => (
141 <>
142 <header>
143 <nav>
144 <Link to="/">Home</Link>
145 <Link to="/about">About</Link>
146 </nav>
147 </header>
148 <Outlet />
149 <TanStackRouterDevtools />
150 </>
151 ),
152})
153```
154<% } %>
155The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
156​
157More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/solid/guide/routing-concepts#layouts).
158​
159## Data Fetching
160​
161There 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.
162​
163For example:
164​
165```tsx
166const peopleRoute = createRoute({
167 getParentRoute: () => rootRoute,
168 path: "/people",
169 loader: async () => {
170 const response = await fetch("https://swapi.dev/api/people");<% if (typescript) { %>
171 return response.json() as Promise<{
172 results: {
173 name: string;
174 }[];
175 }>;
176<% } else { %>
177 return response.json();
178<% } %> },
179 component: () => {
180 const data = peopleRoute.useLoaderData();
181 return (
182 <ul>
183 {data.results.map((person) => (
184 <li key={person.name}>{person.name}</li>
185 ))}
186 </ul>
187 );
188 },
189});
190```
191​
192Loaders simplify your data fetching logic dramatically. Check out more information in the [Loader documentation](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#loader-parameters).
193​
194# Demo files
195​
196Files prefixed with `demo` can be safely deleted. They are there to provide a starting point for you to play around with the features you've installed.
197​
198<% if (addOnEnabled.biome || addOnEnabled.eslint) { %>
199## Linting & Formatting
200<% if (addOnEnabled.biome) { %>
201This project uses [Biome](https://biomejs.dev/) for linting and formatting. The following scripts are available:
202<% } %>
203<% if (addOnEnabled.eslint) { %>
204This project uses [eslint](https://eslint.org/) and [prettier](https://prettier.io/) for linting and formatting. Eslint is configured using [tanstack/eslint-config](https://tanstack.com/config/latest/docs/eslint). The following scripts are available:
205<% } %>
206```bash
207<%= getPackageManagerRunScript('lint') %>
208<%= getPackageManagerRunScript('format') %>
209<%= getPackageManagerRunScript('check') %>
210```
211<% } %>
212​
213# Learn More
214​
215You can learn more about all of the offerings from TanStack in the [TanStack documentation](https://tanstack.com).