Created
July 21, 2017 22:10
-
-
Save jeongsd/c89f927a08cf6c318ea26dbc1a85a5b3 to your computer and use it in GitHub Desktop.
react router constants
This file contains hidden or 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