Skip to content

Commit 343ca20

Browse files
committed
Small updates
1 parent a1ea538 commit 343ca20

File tree

5 files changed

+150
-15
lines changed

5 files changed

+150
-15
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ pnpm i
1010

1111
# Manual testing
1212

13-
| Command | Description |
14-
| ------------------------------------------------------- | ------------------------------------------ |
15-
| `pnpm start app-js` | Creates a JavaScript app |
16-
| `pnpm start app-ts --template typescript` | Creates a TypeScript app |
17-
| `pnpm start app-js-tw --tailwind` | Creates a JavaScript app with Tailwind CSS |
18-
| `pnpm start app-ts-tw --template typescript --tailwind` | Creates a TypeScript app with Tailwind CSS |
13+
| Command | Description |
14+
| -------------------------------------------------- | ------------------------------------------ |
15+
| `npx . app-js` | Creates a JavaScript app |
16+
| `npx . app-ts --template typescript` | Creates a TypeScript app |
17+
| `npx . app-js-tw --tailwind` | Creates a JavaScript app with Tailwind CSS |
18+
| `npx . app-ts-tw --template typescript --tailwind` | Creates a TypeScript app with Tailwind CSS |

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- [x] Add a public directory
22
- [x] Fix the issue with `logo.svg` in build
33
- [x] Provide better feedback when someone asks for a template that isn't `typescript`. We don't support that and never will.
4-
- [ ] Check to make sure it works on Windows
4+
- [x] Check to make sure it works on Windows
55
- [ ] Error handling
66
- [ ] Add tests
7+
- [ ] Better project README

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
{
22
"name": "create-tanstack-app",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "0.0.1",
4+
"description": "Tanstack Application Builder",
55
"bin": "./dist/index.js",
66
"type": "module",
77
"scripts": {
88
"build": "tsc",
99
"start": "tsc && node dist/index.js",
1010
"test": "echo \"Error: no test specified\" && exit 1"
1111
},
12-
"keywords": [],
13-
"author": "",
14-
"license": "ISC",
12+
"keywords": [
13+
"react",
14+
"tanstack",
15+
"router",
16+
"create-react-app"
17+
],
18+
"author": "Jack Herrington <[email protected]>",
19+
"license": "MIT",
1520
"dependencies": {
1621
"@clack/prompts": "^0.10.0",
1722
"commander": "^13.1.0",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

project-template/README.md

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,132 @@
22

33
To run this example:
44

5-
- `npm install` or `yarn`
6-
- `npm start` or `yarn start`
5+
- `npm install` or `yarn` or `pnpm install` or `bun install`
6+
- `npm start` or `yarn start` or `pnpm start` or `bun run dev`
77

88
## Styling
99

1010
This project includes [Tailwind CSS](https://tailwindcss.com/), a utility-first CSS framework.
1111

1212
## Routing
1313

14-
This project uses [TanStack Router](https://tanstack.com/router).
14+
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.tsx 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/react/guide/file-based-routing) guide.
15+
16+
### Adding A Route
17+
18+
To add a new route to your application just add another `createRoute` call to the `./src/main.tsx` file. The example below adds a new `/about`route to the root route.
19+
20+
```tsx
21+
const aboutRoute = createRoute({
22+
getParentRoute: () => rootRoute,
23+
path: "/about",
24+
component: () => <h1>About</h1>,
25+
});
26+
```
27+
28+
You will also need to add the route to the `routeTree` in the `./src/main.tsx` file.
29+
30+
```tsx
31+
const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]);
32+
```
33+
34+
With this set up you should be able to navigate to `/about` and see the about page.
35+
36+
Of course you don't need to implement the About page in the `main.tsx` file. You can create that component in another file and import it into the `main.tsx` file, then use it in the `component` property of the `createRoute` call, like so:
37+
38+
```tsx
39+
import About from "./components/About";
40+
41+
const aboutRoute = createRoute({
42+
getParentRoute: () => rootRoute,
43+
path: "/about",
44+
component: About,
45+
});
46+
```
47+
48+
That is how we have the `App` component set up with the home page.
49+
50+
For 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/react/guide/code-based-routing) documentation.
51+
52+
Now that you have two routes you can use a `Link` component to navigate between them.
53+
54+
### Adding Links
55+
56+
To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
57+
58+
```tsx
59+
import { Link } from "@tanstack/react-router";
60+
```
61+
62+
Then anywhere in your JSX you can use it like so:
63+
64+
```tsx
65+
<Link to="/about">About</Link>
66+
```
67+
68+
This will create a link that will navigate to the `/about` route.
69+
70+
### Using A Layout
71+
72+
Layouts can be used to wrap the contents of the routes in menus, headers, footers, etc.
73+
74+
There is already a layout in the `src/main.tsx` file:
75+
76+
```tsx
77+
const rootRoute = createRootRoute({
78+
component: () => (
79+
<>
80+
<Outlet />
81+
<TanStackRouterDevtools />
82+
</>
83+
),
84+
});
85+
```
86+
87+
You can use the React 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:
88+
89+
```tsx
90+
const rootRoute = createRootRoute({
91+
component: () => (
92+
<>
93+
<header>
94+
<nav>
95+
<Link to="/">Home</Link>
96+
<Link to="/about">About</Link>
97+
</nav>
98+
</header>
99+
<Outlet />
100+
<TanStackRouterDevtools />
101+
</>
102+
),
103+
});
104+
```
105+
106+
The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
107+
108+
## Data Fetching
109+
110+
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.
111+
112+
For example:
113+
114+
```tsx
115+
const peopleRoute = createRoute({
116+
getParentRoute: () => rootRoute,
117+
path: "/people",
118+
loader: async () => {
119+
const response = await fetch("https://swapi.dev/api/people");
120+
return response.json();
121+
},
122+
component: () => {
123+
const data = useLoaderData<typeof loader>();
124+
return (
125+
<ul>
126+
{data.results.map((person) => (
127+
<li key={person.name}>{person.name}</li>
128+
))}
129+
</ul>
130+
);
131+
},
132+
});
133+
```

0 commit comments

Comments
 (0)