diff --git a/docs/api.md b/docs/api.md index e3e55ea..3914747 100644 --- a/docs/api.md +++ b/docs/api.md @@ -33,6 +33,61 @@ Where `action` is just a regular function that may, or may not, return any arbit — a string, a React component, anything! +## `href(routes, routeName[, routeParams])` ⇒ `String|null` + +Traverses the list of routes in the order they are defined until it finds the first route that +matches provided name string. + +```js +// ./routes/index.js +import { href } from 'universal-router'; + +const routes = { + path: '/', + children: [ + { name: 'one', path: '/one', action: () => {} }, + { path: '/two', action: () => {}, children: [ + { path: '/three', action: () => {} } + { name: 'four', path: '/four/:four', action: () => {} } + ] } + ] +}; + +export default routes; + +console.log(href(routes, 'one')); +// => /one + +console.log(href(routes, 'three')); +// => null + +console.log(href(routes, 'four', { four: 'a' })); +// => /two/four/a +``` + +```js +// ./components/Link/index.js +import React from 'react'; +import { href } from 'universal-router'; +import routes from '../../routes'; + +const Link = ({ routeName, routeParams, children }) => ( + {children} +); + +// ./components/Navigation.js +import React from 'react'; +import Link from './components/Link'; + +const Navigation = () => ( + +); +``` + + ## Nested Routes Each route may have an optional `children: [ ... ]` property containing the list of child routes: diff --git a/docs/getting-started.md b/docs/getting-started.md index 2ae1b5b..746189e 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -54,3 +54,26 @@ resolve(routes, { path: '/one' }).then(component => { // renders:

Page One

}); ``` + +## Use href method to generate a path by name + +```js +import React from 'react'; +import { href } from 'universal-router'; + +const routes = [ + { name: 'one', path: '/one', action: () =>

Page One

}, + { name: 'two', path: '/two/:two', action: () =>

Page Two

}, + { path: '*', action: () =>

Not Found

} +]; + +const LinkOne = () => ( + LinkOne +); +// LinkOne + +const LinkTwo = () => ( + LinkTwo +); +// LinkTwo +```