Type Route

Type Route

  • Docs
  • GitHub
  • v0.6.0

›<RouteGroup>

Introduction

  • Getting Started
  • Simple React Example

Guides

  • Code Splitting
  • Complex Route Parameters
  • Custom Link Behavior
  • Custom Query String
  • Data Fetching
  • Nested and Similar Routes
  • No Match (404)
  • Page Layout
  • Preventing Navigation
  • Programmatic Navigation
  • Redirects
  • Rendering Links
  • Route Parameters
  • Scroll Restoration
  • Server Side Rendering
  • Styling of Links for the Currently Active Route
  • Type Route without React
  • Wildcard Routes
  • Previous Release Docs
  • Guide Missing?

API Reference

    <ParameterDefinition>

    • param

    <RouteDefinition>

    • defineRoute
    • extend

    <RouteGroup>

    • createGroup
    • has

    <Route>

    • action
    • href
    • link
    • name
    • params
    • push
    • replace

    <Router>

    • createRouter
    • useRoute
    • RouteProvider
    • routes
    • session

    Types

    • Link
    • QueryStringSerializer
    • Route
    • RouterOpts
    • SessionOpts
    • ValueSerializer

    Miscellaneous

    • noMatch
    • preventDefaultLinkClickBehavior
Edit

<RouteGroup>.has

<RouteGroup>.has(route: Route): boolean

The has function is the only property on the RouteGroup object returned by the createGroup function. It takes a route and returns a boolean.

Example

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

const user = defineRoute(
  {
    userId: param.path.string
  },
  p => `/user/${p.userId}`
);

const { routes } = createRouter({
  home: defineRoute("/"),
  user,
  userSettings: user.extend("/settings"),
  userActivity: user.extend("/activity")
});

const groups = {
  user: createGroup([
    routes.user,
    routes.userSettings,
    routes.userActivity
  ])
}

if (groups.user.has(route)) {
  console.log(route.params.userId);
}

In addition to taking a route and returning a boolean, has works with TypeScript's control flow analysis to properly narrow type of the given route in the appropriate code blocks. In the above example this means you can be sure route.params.userId exists within this code block:

if (groups.user.has(route)) {
  console.log(route.params.userId);
}
← createGroupaction →
Type Route is a Type Hero project  ·  Copyright © 2020