- No need to think about what router to use
- Creating a new route in a specified directory automatically creates a route
- No need to create a route map, handle children and all of that
- Layouts, loading states and route props automatically handled by router
Getting Started
Install in existing app
Install in existing app
Hana Router is automatically added to projects generated using
create-hana-app, but if you already have a React project running Vite, you can easily add the router to your project.1
Install the router
2
Add the vite plugin
Open your Vite config file and add the Hana plugin with optional configA local preview of your documentation will be available at
http://localhost:3000.3
Setup router files
The next step is to create an
src/pages folder which will hold all your routes.If you feel the need to have a root file which you can use for initializing libraries like swr, react query or toast libraries, then you can create an src/pages/_app.(jsx/tsx) file which accepts a bunch of children and displays them4
Update index.html file
Every Vite project ships with an And…that’s it!
index.html file which loads the entire application. To switch your application to Hana router, you need to update the entry script to .hana/_app.tsx, don’t worry this script will be automatically created by hana on build/devsrc/pages/home.tsx file:
home.tsx
/home in your browser. It’s that straightforward. Hana will use the slugified version of the file name as the route name without the extension.
Ignored Files
We mentioned that every JavaScript or TypeScript file in thepages directory is compiled into a route. However, there are some files that are ignored by the router. These files are:
- Files that start with an underscore (
_) - Files that end with
.d.ts
Index Routes
Hana automatically maps index files (index.jsx/index.tsx) to the directory name. For example, if you create a file calledindex.tsx in the pages/home directory, it will create a route for /home. An index.jsx file in the pages directory will create a route for /.
pages/index.tsx->/pages/home/index.jsx->/home
Nested Routes
The router allows you to nest your components within folders to create nested routes. For example, you can add a new/dashboard/settings route by nesting a settings.tsx file inside a dashboard folder or by an index.tsx file inside a settings folder inside a dashboard folder.
pages/dashboard/settings.tsx->/dashboard/settingspages/dashboard/settings/index.tsx->/dashboard/settings
Dynamic Routing
Dynamic routing is the idea of creating routes that aren’t fixed. For example, leaf’s x.com profile is x.com/leafphp, but yours is x.com/username. Since there’s no real way for X to know what username you’d set, your profile link can’t be hardcoded, and so we call that a dynamic route. Hana’s router provides everything you’ll need for dynamic routing right out of the box, and you can find the full documentation hereNavigating between pages
After you’ve added your routes, the next thing is creating a link between them, whether it’s through a navbar, footer or redirects, you can do so easily:Setup Files
Setup files are files that start with an underscore (_). These files are used to set up different functionality for your app. For example, you can use a setup file to set up global styles or components that should be available to all routes.
_app.tsx
The_app.tsx file is used to set up global styles or components that should be available to all routes. It is also used to wrap all routes in a layout component. This file is heavily inspired by Next.js’ _app.tsx file. The example below sets up Hana Store.
src/pages/_app.tsx

