Skip to content

Instantly share code, notes, and snippets.

@jeongsd
Created July 21, 2017 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeongsd/c89f927a08cf6c318ea26dbc1a85a5b3 to your computer and use it in GitHub Desktop.
Save jeongsd/c89f927a08cf6c318ea26dbc1a85a5b3 to your computer and use it in GitHub Desktop.
react router constants
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