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

createGroup

createGroup(routes: (RouteBuilder | RouteGroup)[]): RouteGroup

The createGroup function takes an array of RouteBuilder and RouteGroup objects and returns a RouteGroup. The createGroup function is useful for composing groups of routes to make checking against them easier elsewhere in the application with the has function. It takes an array composed of both RouteBuilder and RouteGroup objects. Read the Nested/Similar Routes guide for more information.

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("/"),
  userSummary: user.extend("/"),
  userSettings: user.extend("/settings"),
  userPostList: user.extend("/post"),
  userPost: user.extend(
    {
      postId: param.path.string
    },
    p => `/post/${p.postId}`
  )
});

const postGroup = createGroup([routes.userPostList, routes.userPost]);

const groups = {
  post: postGroup,
  user: createGroup([
    routes.userSummary,
    routes.userSettings,
    postGroup
  ])
};

const route = session.getInitialRoute();

if (groups.user.has(route)) {
  console.log(route.name;) // "userSummary" | "userSettings" | "userPostList" | "userPost"
}
← extendhas →
Type Route is a Type Hero project  ·  Copyright © 2020