Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zilch/type-route

The flexible, type safe routing library.
https://github.com/zilch/type-route

Last synced: 12 days ago
JSON representation

The flexible, type safe routing library.

Awesome Lists containing this project

README

        







Type Route




Type Route



Quick Start  ·  Docs



## Getting Started

Type Route is a flexible, type safe routing library built on top of the same [core library](https://github.com/ReactTraining/history) that powers React Router.

> **Type Route was designed with excellent React integration in mind** but isn't coupled to a specific UI framework. Most code examples in the documentation use React, but the general principles covered apply regardless of framework.

Continue reading this introduction for a quick overview of how to start using Type Route in your React project. Find a full runnable version of the below introduction on the [Simple React Example](https://type-route.zilch.dev/introduction/simple-react-example) page or see the [Type Route without React](https://type-route.zilch.dev/guides/type-route-without-react) guide to learn how to use Type Route without React.

## Install

Type Route's primary distribution channel is the [NPM registry](https://www.npmjs.com/package/type-route). React `16.8` (or any subsequent version of React) is a peer dependency of Type Route so you'll need to ensure that's installed as well.

```bash
npm install type-route react
```

## Step 1: Create a Router

`router.ts`

```typescript
import { createRouter, defineRoute, param } from "type-route";

export const { RouteProvider, useRoute, routes } = createRouter({
home: defineRoute("/"),
userList: defineRoute(
{
page: param.query.optional.number,
},
() => "/user"
),
user: defineRoute(
{
userId: param.path.string,
},
(p) => `/user/${p.userId}`
),
});
```

Best practice is to immediately destructure the result of [`createRouter`](https://type-route.zilch.dev/api-reference/router/create-router) into the properties you'll be using in your application. The [`createRouter`](https://type-route.zilch.dev/api-reference/router/create-router) function accepts an object with route names and route definitions created via [`defineRoute`](https://type-route.zilch.dev/api-reference/route-definition/define-route) and returns a new router.

## Step 2: Connect Router to Application

`App.tsx`

```tsx {17-19}
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import { RouteProvider } from "./router";
import { Page } from "./Page";
import { Navigation } from "./Navigation";

function App() {
return (
<>


>
);
}

ReactDOM.render(


,
document.querySelector("#root")
);
```

Wrap your entire application in the `` component returned by [`createRouter`](https://type-route.zilch.dev/api-reference/router/create-router).

## Step 3: Display Current Route

`Page.tsx`

```tsx
import React from "react";
import { useRoute, routes } from "./router";
import { HomePage } from "./HomePage";
import { UserListPage } from "./UserListPage";
import { UserPage } from "./UserPage";
import type { Route } from "type-route";

export function Page() {
const route = useRoute();

return (
<>
{route.name === "home" && }
{route.name === "userList" && }
{route.name === "user" && }
{route.name === false && "Not Found"}
>
);
}

function HomePage() {
return

Home Page
;
}

function UserListPage({ route }: { route: Route }) {
return

UserList Page: {route.params.page}
;
}

function UserPage({ route }: { route: Route }) {
return

User: {route.params.userId}
;
}
```

Inside the code blocks above the TypeScript compiler (and your editor) will be able to correctly infer the type of `route`. This allows you, for instance, to pass the `user` route to the `UserPage` component and access the `userId` param with confidence in code blocks where it will definitely exist.

> Type Route is written in TypeScript and designed for TypeScript users. Any editor, however, whose JavaScript experience is powered by TypeScript (VSCode for instance) will provide many of the same benefits described here when using regular JavaScript.

## Step 4: Navigate Between Routes

`Navigation.tsx`

```tsx
import React from "react";
import { routes } from "./router";

export function Navigation() {
return (

Home
User List
User List Page 2
User "abc"

);
}
```

The [`link`](https://type-route.zilch.dev/api-reference/route/link) property is an object with an `href` attribute and an `onClick` function. You need both to [properly render](https://type-route.zilch.dev/guides/rendering-links) a link in a single page application. Immediately spreading the `link` object into the properties of an `` tag makes usage simple. [Programmatic navigation](https://type-route.zilch.dev/guides/programmatic-navigation) is possible with the [`push`](https://type-route.zilch.dev/api-reference/route/push) and [`replace`](https://type-route.zilch.dev/api-reference/route/replace) functions of a specific route. Type Route also supports [extending the behavior of a link](https://type-route.zilch.dev/guides/custom-link-behavior) to cover more complex scenarios.

## Next Steps

Hopefully that was enough to point you in the right direction!

If you need more guidance there is a full runnable version of the above code on the [Simple React Example](https://type-route.zilch.dev/introduction/simple-react-example) page. The **Guides** section of the documentation has detailed overviews and examples for most use cases. Additionally, the **API Reference** section has descriptions and examples for each part of the API.

[View Docs →](https://type-route.zilch.dev/)