@@ -8,33 +8,31 @@ title: API ∙ Universal Router
88## ` const router = new Router(routes, options) `
99
1010Creates an universal router instance which have a single
11- [ ` router.resolve() ` ] ( ## routerresolve-path-context---promiseany ) method.
12- ` Router ` constructor expects a plain javascript object for the first argument
13- with any amount of params where only ` path ` is required or array of such objects.
14- Second argument is optional or you can pass following:
11+ [ ` router.resolve() ` ] ( #routerresolve-path-context---promiseany ) method.
12+ ` Router ` constructor expects a plain javascript object for the first ` routes ` argument
13+ with any amount of params where only ` path ` is required, or array of such objects.
14+ Second ` options ` argument is optional where you can pass the following:
1515
16- - ` context ` - The object with any data which you want to pass through to ` resolveRoute ` function.<br >
16+ - ` context ` - The object with any data which you want to pass to ` resolveRoute ` function.<br >
1717 See [ Context] ( #context ) section below for details.
18- - ` baseUrl ` - The base URL of the app. Empty string ` '' ` by default .<br >
18+ - ` baseUrl ` - The base URL of the app. By default is empty string ` '' ` .<br >
1919 If all the URLs in your app are relative to some other "base" URL, use this option.
2020- ` resolveRoute ` - The function for any custom route handling logic.<br >
2121 For example you can define this option to work with routes in declarative manner.<br >
22- By default the router calls ` action ` methods of matched route if any.
23-
24- Example:
22+ By default the router calls the ` action ` function of matched route.
2523
2624``` js
2725import Router from ' universal-router' ;
2826
2927const routes = {
30- path: ' /' , // string, required
31- name: ' home' , // unique string, optional
32- parent: null , // route object or null, optional ( filled by router)
33- children: [], // array of route objects or null, optional
34- action (context , params ) { // function, optional
35-
36- // method should return anything except `null` or `undefined` to be resolved by router
37- // otherwise router will throw an error if all matched routes returned nothing
28+ path: ' /' , // string, required
29+ name: ' home' , // unique string, optional
30+ parent: null , // route object or null, automatically filled by the router
31+ children: [], // array of route objects or null, optional
32+ action (context , params ) { // function, optional
33+
34+ // action method should return anything except `null` or `undefined` to be resolved by router
35+ // otherwise router will throw `Page not found` error if all matched routes returned nothing
3836 return ' <h1>Home Page</h1>' ;
3937 },
4038 // ...
@@ -58,7 +56,7 @@ const router = new Router(routes, options);
5856## ` router.resolve({ path, ...context }) ` ⇒ ` Promise<any> `
5957
6058Traverses the list of routes in the order they are defined until it finds the first route that
61- matches provided URL path string and whose action method returns anything other than ` null ` or ` undefined ` .
59+ matches provided URL path string and whose ` action ` function returns anything other than ` null ` or ` undefined ` .
6260
6361``` js
6462const router = new Router ([
@@ -150,8 +148,8 @@ Also check out online [router tester](http://forbeslindesay.github.io/express-ro
150148
151149## Context
152150
153- In addition to a URL path string, any arbitrary data can be passed to the router's ` resolve() ` method,
154- that becomes available inside action methods .
151+ In addition to a URL path string, any arbitrary data can be passed to the ` router. resolve()` method,
152+ that becomes available inside ` action ` functions .
155153
156154``` js
157155const router = new Router ({
@@ -166,29 +164,17 @@ router.resolve({ path: '/hello', user: 'admin' })
166164 // => Welcome, admin!
167165```
168166
169- Router supports ` context ` option in the constructor
167+ Router supports ` context ` option in the ` Router ` constructor
170168to support for specify of custom context properties only once.
171169
172170``` js
173- import { createStore } from ' redux' ;
174-
175- const route = {
176- path: ' /hello' ,
177- action (context ) {
178- const state = context .store .getState ();
179- return ` Welcome, ${ state .user } !` ;
180- },
181- };
182-
183171const context = {
184- store: createStore ({ user: ' admin' }),
172+ store: {},
173+ user: ' admin' ,
174+ // ...
185175};
186176
187177const router = new Router (route, { context });
188-
189- router .resolve ({ path: ' /hello' })
190- .then (result => console .log (result));
191- // => Welcome, admin!
192178```
193179
194180Router always adds following parameters to the ` context ` object
@@ -199,7 +185,7 @@ before passing it to the `resolveRoute` function:
199185- ` next ` - Middleware style function which can continue resolving,
200186 see [ Middlewares] ( #middlewares ) section below for details.
201187- ` url ` - URL which was transmitted to ` router.resolve() ` .
202- - ` baseUrl ` - Base URL path relative to the current route.
188+ - ` baseUrl ` - Base URL path relative to the path of the current route.
203189- ` path ` - Matched path.
204190- ` params ` - Matched path params,
205191 see [ URL Parameters] ( #url-parameters ) section above for details.
@@ -274,7 +260,7 @@ router.resolve({ path: '/hello' });
274260```
275261
276262Remember that ` context.next() ` iterates only child routes,
277- use ` context.next(true) ` to iterate through all remaining routes.
263+ use ` context.next(true) ` to iterate through the all remaining routes.
278264
279265
280266## URL Generation
@@ -289,7 +275,7 @@ In most web applications it's much simpler to just use a string for hyperlinks.
289275// etc.
290276```
291277
292- However for some types of web applications it may be useful to generate URLs dynamically based on route names .
278+ However for some types of web applications it may be useful to generate URLs dynamically based on route name .
293279That's why this feature is available as an extension ` url(routeName, params) ⇒ String ` .
294280
295281``` js
@@ -314,7 +300,7 @@ url('home'); // => '/base'
314300url (' hello' , { username: ' john' }); // => '/base/hello/john'
315301```
316302
317- This approach also works fine for dynamically added routes at runtime:
303+ This approach also works fine for dynamically added routes at runtime.
318304
319305``` js
320306routes .children .push ({ path: ' /world' , name: ' world' });
0 commit comments