Reach equal standing with starting template from RRv7.

This commit is contained in:
2025-03-29 20:41:49 +01:00
parent 58620c057e
commit 584ced1cd5
11 changed files with 928 additions and 137 deletions

View File

@@ -1,4 +1,11 @@
Welcome to your new TanStack app!
# TanStack/Router Address Book "RRv7 Tutorial"
This is my implementation of the React Router v7 tutorial you can
find [here](https://reactrouter.com/tutorials/address-book).
This is the
WaybackMachine: [here](https://web.archive.org/web/20250130112003/https://reactrouter.com/tutorials/address-book) at
2025-01-30.
# Getting Started
@@ -29,11 +36,10 @@ pnpm test
This project uses CSS for styling.
## Routing
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 files in `src/routes`.
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 files in `src/routes`.
### Adding A Route
@@ -45,7 +51,8 @@ Now that you have two routes you can use a `Link` component to navigate between
### Adding Links
To use SPA (Single Page Application) navigation you will need to import the `Link` component from `@tanstack/react-router`.
To use SPA (Single Page Application) navigation you will need to import the `Link` component from
`@tanstack/react-router`.
```tsx
import { Link } from "@tanstack/react-router";
@@ -59,11 +66,13 @@ Then anywhere in your JSX you can use it like so:
This will create a link that will navigate to the `/about` route.
More information on the `Link` component can be found in the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
More information on the `Link` component can be found in
the [Link documentation](https://tanstack.com/router/v1/docs/framework/react/api/router/linkComponent).
### Using A Layout
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.
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.
Here is an example layout that includes a header:
@@ -82,8 +91,8 @@ export const Route = createRootRoute({
<Link to="/about">About</Link>
</nav>
</header>
<Outlet />
<TanStackRouterDevtools />
<Outlet/>
<TanStackRouterDevtools/>
</>
),
})
@@ -91,12 +100,14 @@ export const Route = createRootRoute({
The `<TanStackRouterDevtools />` component is not required so you can remove it if you don't want it in your layout.
More information on layouts can be found in the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
More information on layouts can be found in
the [Layouts documentation](https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#layouts).
## Data Fetching
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.
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.
For example:
@@ -125,11 +136,13 @@ const peopleRoute = createRoute({
});
```
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).
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).
### React-Query
React-Query is an excellent addition or alternative to route loading and integrating it into you application is a breeze.
React-Query is an excellent addition or alternative to route loading and integrating it into you application is a
breeze.
First add your dependencies:
@@ -153,7 +166,7 @@ if (!rootElement.innerHTML) {
root.render(
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
<RouterProvider router={router}/>
</QueryClientProvider>
);
}
@@ -167,9 +180,9 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
const rootRoute = createRootRoute({
component: () => (
<>
<Outlet />
<ReactQueryDevtools buttonPosition="top-right" />
<TanStackRouterDevtools />
<Outlet/>
<ReactQueryDevtools buttonPosition="top-right"/>
<TanStackRouterDevtools/>
</>
),
});
@@ -206,11 +219,13 @@ function App() {
export default App;
```
You can find out everything you need to know on how to use React-Query in the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
You can find out everything you need to know on how to use React-Query in
the [React-Query documentation](https://tanstack.com/query/latest/docs/framework/react/overview).
## State Management
Another common requirement for React applications is state management. There are many options for state management in React. TanStack Store provides a great starting point for your project.
Another common requirement for React applications is state management. There are many options for state management in
React. TanStack Store provides a great starting point for your project.
First you need to add TanStack Store as a dependency:
@@ -241,7 +256,8 @@ function App() {
export default App;
```
One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will update when the base state updates.
One of the many nice features of TanStack Store is the ability to derive state from other state. That derived state will
update when the base state updates.
Let's check this out by doubling the count using derived state.
@@ -275,15 +291,19 @@ function App() {
export default App;
```
We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount` method that will start the derived store updating.
We use the `Derived` class to create a new store that is derived from another store. The `Derived` class has a `mount`
method that will start the derived store updating.
Once we've created the derived store we can use it in the `App` component just like we would any other store using the `useStore` hook.
Once we've created the derived store we can use it in the `App` component just like we would any other store using the
`useStore` hook.
You can find out everything you need to know on how to use TanStack Store in the [TanStack Store documentation](https://tanstack.com/store/latest).
You can find out everything you need to know on how to use TanStack Store in
the [TanStack Store documentation](https://tanstack.com/store/latest).
# Demo files
Files 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.
Files 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.
# Learn More