Tanstack offers an advanced development experience in recent times. Today, I will introduce the Tanstack Routing System.
Compared to the React routing system, it has exciting features. The Tanstack Routing System is a modern routing solution that comes with TypeScript support, nested routing, and layouts. It also provides TypeScript JSON-first Search Params state management APIs. Since it is a modern routing system that emerged recently, it utilizes 100% inferred TypeScript support.
How Tanstack Route Creation Happens?
A route is automatically created when a new file is added, with the route name derived from the folder structure.
Let's understand this with an example. We can create both normal files and lazy files by using the .lazy syntax to support lazy loading.To fetch data from parameters, we use the $id syntax in the file name
Root Route
import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/router-devtools'
export const Route = createRootRoute({
component: () => (
<>
<div className="p-2 flex gap-2">
<Link to="/" className="[&.active]:font-bold">
Home
</Link>{' '}
<Link to="/about" className="[&.active]:font-bold">
About
</Link>
</div>
<hr />
<Outlet />
<TanStackRouterDevtools />
</>
),
})
Using lazy route
import { createLazyFileRoute } from '@tanstack/react-router'
export const Route = createLazyFileRoute('/about')({
component: About,
})
function About() {
return <div className="p-2">Hello from About!</div>
}
Using path path(file name start with ${paramsID))
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => {
return fetchPost(params.postId)
},
})
Installation Process
We can install Tanstack Router using various package managers like npm, yarn, pnpm, or bun. After installation, we configure the router in our Vite config if we are using the Vite run engine.
There are several other features available in Tanstack Router that help developers enhance routing efficiency.
Tanstack vs. React Router Use Cases
Both Tanstack Router and React Router have their own use cases and advantages. Here’s a quick comparison:
Advantages of Tanstack Router Over React Router
Automatic route generation based on the folder structure.
TypeScript-first approach for better type safety.
Built-in search params management using JSON.
Nested layouts and routes for better organization.
Lazy loading support for better performance.
Efficient state management APIs integrated within the router.
When Should You Use Tanstack Router Instead of React Router??
If you need strong TypeScript support with automatic type inference.
When your project requires advanced route-based state management.
If you prefer automatic route creation based on folder structures.
When working on large-scale applications with deeply nested layouts.
For more details, check the official documentation of Tanstack Router Documentation.
**
Conclusion
**
The Tanstack Routing System provides a modern and efficient approach to handling routes in React applications. With its TypeScript-first approach, automatic route creation, and improved state management, it offers a more structured and scalable solution compared to traditional React routing methods. If you're looking for a powerful and developer-friendly routing system, Tanstack Router is definitely worth exploring!
Top comments (0)