react router constants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathToRegexp from 'path-to-regexp'; | |
class RouteConst { | |
constructor(path, parent = null) { | |
this.path = parent ? `${parent.path}${path}` : path; | |
this.toURL = pathToRegexp.compile(this.path); | |
} | |
} | |
export const HOME = new RouteConst('/'); | |
export const ABOUT = new RouteConst('about', HOME); | |
export const USERS = new RouteConst('users', HOME); | |
export const USER = new RouteConst('/:id', USERS); | |
// USERS.path = /users | |
// USER.path = /users/:id | |
// USER.toURL({ id: 1 }) = /users/1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment