Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

ranked-routes.md

95 lines1.4 KB

A simple utility for ranking routes based on their specificity

Installation

yarn add @tanstack/react-location-rank-routes
# or
npm i @tanstack/react-location-rank-routes

Getting Started

import { ReactLocation, Router } from '@tanstack/react-location'
import { rankRoutes } from '@tanstack/react-location-rank-routes'

//

const location = new ReactLocation()

function App() {
  return (
    <Router
      location={location}
      filterRoutes={rankRoutes}
      routes={[
        {
          path: '/',
        },
        {
          path: '/todos',
        },
        {
          path: '/todos/:todoId',
          children: [
            {
              path: '/',
            },
            {
              path: '/tasks',
            },
            {
              path: '/tasks/:taskId',
            },
            {
              path: '/',
            },
          ],
        },
        {
          path: '/',
        },
      ]}
    />
  )
}

The Routes above will be filtered and ranked in the following order:

const routes = [
  {
    path: '/',
  },
  {
    path: '/todos/:todoId',
    children: [
      {
        path: '/',
      },
      {
        path: '/tasks/:taskId',
      },
      {
        path: '/tasks',
      },
      {
        path: '/*',
      },
    ],
  },
  {
    path: '/todos',
  },
  {
    path: '/*',
  },
]