Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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