cyclic-router
cyclic-router V5 is a routing library built for Cycle.js. Unlike previous versions, cyclic-router V5 is not a driver. It is a main
function-wrapper (routerify) which relies on @cycle/history for driver source/sink interactions.
For older versions of cyclic-router, V4 and earlier please go to the old README
Installation
Using npm:
$ npm install --save @cycle/history # @cycle/history is a peerDependency of cyclic-router
$ npm install --save-dev @types/history # @cycle/history uses ReactTraining/history
# under the hood (For Typescript users only)
$ npm install --save cyclic-router
Routerify requires you to inject the route matcher. We'll use switch-path
for our examples but other matching libraries could be adapted to be used here:
$ npm install --save switch-path
// using an ES6 transpiler, like babel // not using an ES6 transpilervar routerify = routerify
Basic Usage
;;;;;; { const pageSinks$ = sourcesroutersources; return DOM: pageSinks$ router: xs // Notice use of 'router' sink name, // which proxies the original 'history' sink ;} const mainWithRouting = ;
Routerify Options
Routerify accepts an options object like so: routerify(main, switchPath, options)
The options
object conforms to the interface:
interface RouterOptions {
basename?: string;
historyName?: string;
routerName?: string;
omitHistory?: boolean;
}
basename
- The root router path, defaults to/
historyName
- The source/sink name associated with the raw history driver, defaults tohistory
routerName
- The source/sink name you want exposed to your app which captures the functionality/streams associated with routerify. Defaults torouter
omitHistory
- Routerify hides the source/sink name associated with the raw history driver (given in thehistoryName
option) from your app and only exposes the source/sink name associated with the router (given in therouterName
option). Defaults totrue
, If you would like your app to have access to both the rawhistory
source/sink and therouter
source/sink (injected by routerify), set this option tofalse
Route Parameters
This behavior changes based on the injected route matcher. In the case of switch-path
, you can pass route parameters to your component by adding them to the component sources.
const routes =
Dynamically change route
You can dynamically change route from code by emitting inputs handled by the history driver.
... { // ... const homePageClick$ = sourcesDOM; const previousPageClick$ = sourcesDOM; const nextPageClick$ = sourcesDOM; const oldPageClick$ = sourcesDOM; const aboutPageClick$ = sourcesDOM; const replacePageClick$ = sourcesDOM; return // ... router: xs