Skip to content

swan-io/chicane

Folders and files

NameName
Last commit message
Last commit date

Latest commit

9343316 Β· Feb 2, 2023
Mar 27, 2022
Dec 30, 2022
Dec 30, 2022
Dec 30, 2022
Feb 2, 2023
Oct 23, 2021
Oct 23, 2022
Oct 23, 2022
Oct 23, 2021
Aug 10, 2022
Nov 24, 2022
Oct 23, 2021
Nov 24, 2022
Feb 2, 2023
May 24, 2022
Dec 30, 2022
Oct 4, 2022
Dec 30, 2022

Repository files navigation

@swan-io/chicane logo

@swan-io/chicane

mit licence npm version bundlephobia

A simple and safe router for React and TypeScript.

Design principles

  • Typed routes: improving the DX, and making sure all your params are here!
  • Component-friendly: the router nicely fits in your React app.
  • Easy-to-use: naming routes instead of moving around unsafe URLs.
  • Performant: avoids any unnecessary render.

Installation

$ yarn add @swan-io/chicane
# --- or ---
$ npm install --save @swan-io/chicane

Links

Quickstart

import { createRouter } from "@swan-io/chicane";
import { match } from "ts-pattern";

const Router = createRouter({
  Home: "/",
  Users: "/users",
  User: "/users/:userId",
});

const App = () => {
  const route = Router.useRoute(["Home", "Users", "User"]);

  // route object is a discriminated union
  return match(route)
    .with({ name: "Home" }, () => <h1>Home</h1>)
    .with({ name: "Users" }, () => <h1>Users</h1>)
    .with({ name: "User" }, ({ params }) => <h1>User {params.userId}</h1>) // params are strongly typed
    .otherwise(() => <h1>404</h1>);
};

Run the example app

$ git clone [email protected]:swan-io/chicane.git
$ cd chicane/example

$ yarn install && yarn dev
# --- or ---
$ npm install && npm run dev

πŸ™Œ Acknowledgements