Skip to content

Instantly share code, notes, and snippets.

@panta82
Last active November 30, 2017 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panta82/d691ec06532e0ad6b38eda9267baa0d8 to your computer and use it in GitHub Desktop.
Save panta82/d691ec06532e0ad6b38eda9267baa0d8 to your computer and use it in GitHub Desktop.
React Router annotations
**/
createLocation(location) {}
/**
* This seems to be the same as createHref, only without prepending #/ and base path
* @param {RouterLocation|string} location
* @returns string
**/
createPath(location) {}
/**
* Returns current location
* @returns {RouterLocation}
*/
getCurrentLocation() {}
/**
* Wraps native History.go. Go forwards or backwards n steps (negative for back).
* Do not use with hashHistory.
* @param {Number} n
*/
go(n) {}
/**
* Wrapper around go(-1)
*/
goBack() {}
/**
* Wrapper around go(1)
*/
goForward() {}
/**
* @callback routerLocationListener
* @param {RouterLocation} location
*/
/**
* History will call the function you give it every time location changes.
* It returns the unsubscribe function, you can call to make it stop
* @param {routerLocationListener} listener
* @returns function
*/
listen(listener) {}
/**
* @callback routerLocationHook
* @param {RouterLocation} location
* @param {function} done
*/
/**
* The same as listen(), except you are called with the route you are about to leave.
* Also, you have to call done to proceed. I guess you can also call with an alternative location? It's not super clear.
* @param {routerLocationHook} hook
* @returns function
*/
listenBefore(hook) {}
/**
* Navigate to a new location and make it so it appears in browser history (so that pressing back
* will lead you back to your current location).
* @param {RouterLocation} location
*/
push(location) {}
/**
* Navigate to a new location, but without influencing browser history. Only the URL in URL bar will change,
* history list will remain unchanged.
* @param {RouterLocation} location
*/
replace(location) {}
/**
* Backing method behind both push and replace (the only difference is they enforcing the "action" field on Location)
* @param {RouterLocation} nextLocation
*/
transitionTo(nextLocation) {}
}
export class RouteDefinition {
constructor(source) {
/**
* List of child route definitions
* @type {RouteDefinition[]}
*/
this.childRoutes = [];
/**
* Component that will be rendered at this route
* @type {function}
*/
this.component = null;
/**
* Index route for this route (at the same level?)
* @type {RouteDefinition}
*/
this.indexRoute = null;
/**
* Name given to this route
* @type {string}
*/
this.name = '';
/**
* Path of this route (with placeholders, eg. /users/:userId/profile)
* @type {string}
*/
this.path = '';
}
}
export class Router extends RouterHistory {
isActive(location, indexOnly) {}
setRouteLeaveHook(route, hook) {}
constructor(source) {
super();
/**
* The same as calling getCurrentLocation(), it seems
* @type RouterLocation
*/
this.location = {};
/**
* Parsed route params from the pathname
* @type {Object.<string, string>}
*/
this.params = {};
/**
* List of routes on stack, starting from home root ('/'), all the way down to your current location.
* You might use this to build a breadcrumbs view.
* @type {RouteDefinition[]}
*/
this.routes = [];
Object.assign(this, source);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment