Tan Stack Router is going to replace React Router

Samad
2 min readDec 22, 2022

--

Tan Stack Router is a new type-safe router for JavaScript applications that aims to solve the common problem of type safety in routing. With Tan Stack Router, developers can enjoy the benefits of type safety and autocomplete suggestions while working with routes in their applications.

One of the main advantages of Tan Stack Router is that it provides a clear connection between the routes that are defined and the places where they are used. This means that if a route is changed or misspelled in one place, the router will catch the error and provide a helpful suggestion or error message. For example, consider the following code:

import { createRouter, useRouter } from 'tanstack-router';

const routes = {
'/': () => <Home />,
'/about': () => <About />,
'/contact': () => <Contact />
};

const Router = createRouter(routes);

function App() {
const { path, navigate } = useRouter();

return (
<div>
<nav>
<button onClick={() => navigate('/')}>Home</button>
<button onClick={() => navigate('/about')}>About</button>
<button onClick={() => navigate('/contact')}>Contact</button>
</nav>
<Router />
</div>
);
}

In this example, the routes object defines the different routes that are available in the application. The createRouter function is then used to create a router component that will display the appropriate content based on the current path. The useRouter hook is used to access the current path and a function to navigate to other routes.

One of the benefits of using Tan Stack Router is that the navigate function will provide autocomplete suggestions for the available routes. For example, if you start typing navigate('/a'), you will see a suggestion for /about. This can save a lot of time and reduce the likelihood of mistakes when working with routes.

Another advantage of Tan Stack Router is that it has support for 100% TypeScript. This means that you can take advantage of type safety and improved autocomplete features when defining your routes. For example, you can specify the type of the parameters that are passed to a route:

const routes = {
'/users/:id': ({ id }: { id: string }) => <UserProfile id={id} />
};

In this case, the id parameter is expected to be a string, and the router will provide an error if it is used with any other type. This helps to ensure that your routes are always used correctly and that you have a clear understanding of the data that is being passed to them.

Overall, Tan Stack Router is a powerful tool for improving the type safety and stability of your application’s routing. With its autocomplete suggestions and TypeScript support, it can help you write cleaner, more reliable code and reduce the time you spend debugging issues with your routes. If you’re looking to improve the routing in your application, it’s definitely worth checking out.

--

--