2.1.55 • Published 8 months ago

@slimr/router v2.1.55

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

🪶 @slimr/router npm package

A tiny alternative to react-router with some novel features like stack routing and scroll restore

Features:

  • No dependencies, 3kb min+gziped
  • Stack routing -- a route "stack" can have it's own history like react native's react-navigation
  • Auto handles all link clicks to same-site and back/popstate without needing to use a Link component
  • Fosters/facilitates type-safe linking (no broken links!)
  • Attempts to scroll restore -- usually works so long as your pages use stale-while-refresh
  • Less is more: faster, less bugs, no breaking changes

Context

@slimr is a set of slim React (hence '@slimr') libs. Check them all out on github!

Usage

For a more complete example, please see demo in repo at packages/demo. Also, do refer to the extensive in-code comments.

Basic Example:

// app.tsx
import {Switch} from '@slimr/router'
import {router} from './router'

export function App() {
  return (
    <Switch router={router} />
  )
}

// router.tsx
import {Router, Switch} from '@slimr/router'

import Home from './pages/index'
import Hello from './pages/hello'
import NotFound from './pages/not-found'


export const router = new Router(
  {
  index: {
    component: Home,
    path: '/',
  },
  hello: {
    component: Hello,
    path: '/hello/:name',
  },
  notFound: {
    exact: false,
    component: NotFound,
    path: '/',
  },
  },
  { }
)

// pages/index.tsx
import {setPageMeta} from '@slimr/util'
import {router as r} from '../router'

export default function Index() {
  const {title, description} = setPageMeta({title: 'Home'})
  const helloHref = r.routes.hello.toPath({name: 'world'})
  return (
    <>
      <h1>Home</h1>
      <a href={helloHref}>Goto Hello World</a>
    </>
  )
}

// pages/hello.tsx
import {setPageMeta} from '@slimr/util'
import type {RouteMatch} from '@slimr/router'

export default function Hello({route}: {route: RouteMatch}) {
  const {title, description} = setPageMeta({
    title: `Hello ${route.urlParams!.name}`,
    description: 'A demo of route with url params.',
  })
  return (
    <>
      <h1>{title}</h1>
      <p>{description}</p>
    </>
  )
}

// pages/not-found.tsx
import {setPageMeta} from '@slimr/util'

export default function NotFound() {
  const {title, description} = setPageMeta({
    title: '404: Not Found',
    description: "Sorry, we can't find that page",
  })
  return (
    <>
      <h1>{title}</h1>
      <p>{description}</p>
    </p>
  )
}

Comparisons

coming soon

react-router

  • A popular react router

Pros

  • More mature, SSR support

Cons

  • Is bigger bundle than it should be
  • No stack route support

nextjs router

  • A popular file-system based router

Pros

  • Less setup

Cons

  • Requires using nextjs
  • No stack route support
  • Very inflexible

react-navigation

  • A popular react native router

Pros

  • Very flexible and feature rich

Cons

  • Is overly hard to learn and maintain
  • If using in web, is HUGE bundle impact
2.1.49

8 months ago

2.1.47

8 months ago

2.1.48

8 months ago

2.1.43

8 months ago

2.1.41

8 months ago

2.1.42

8 months ago

2.1.40

8 months ago

2.1.54

8 months ago

2.1.55

8 months ago

2.1.52

8 months ago

2.1.53

8 months ago

2.1.50

8 months ago

2.1.51

8 months ago

2.1.29

9 months ago

2.1.38

9 months ago

2.1.39

8 months ago

2.1.36

9 months ago

2.1.37

9 months ago

2.1.34

9 months ago

2.1.35

9 months ago

2.1.32

9 months ago

2.1.33

9 months ago

2.1.30

9 months ago

2.1.31

9 months ago

2.1.18

1 year ago

2.1.19

1 year ago

2.1.27

1 year ago

2.1.28

1 year ago

2.1.25

1 year ago

2.1.26

1 year ago

2.1.23

1 year ago

2.1.6

1 year ago

2.1.24

1 year ago

2.1.5

1 year ago

2.1.21

1 year ago

2.1.8

1 year ago

2.1.22

1 year ago

2.1.7

1 year ago

2.1.20

1 year ago

2.1.9

1 year ago

2.1.16

1 year ago

2.1.17

1 year ago

2.1.15

1 year ago

2.1.12

1 year ago

2.1.13

1 year ago

2.1.10

1 year ago

2.1.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago