|
| 1 | +API: Router Context |
| 2 | +=================== |
| 3 | + |
| 4 | +The `context` feature of React is undocumented because its not |
| 5 | +completely baked yet, however, it is commited to by the React team and |
| 6 | +we use it with great effect in the Router. There are plans to make |
| 7 | +reliance on context optional, but for now, this is what we use. |
| 8 | + |
| 9 | +You access the router inside of route handlers with |
| 10 | +`this.context.router`. Its got a few useful methods on it. |
| 11 | + |
| 12 | +### `transitionTo(routeNameOrPath, [params[, query]])` |
| 13 | + |
| 14 | +Programmatically transition to a new route. |
| 15 | + |
| 16 | +#### Examples |
| 17 | + |
| 18 | +```js |
| 19 | +this.context.router.transitionTo('user', {id: 10}, {showAge: true}); |
| 20 | +this.context.router.transitionTo('about'); |
| 21 | +this.context.router.transitionTo('/users/10?showAge=true'); |
| 22 | +this.context.router.transitionTo('http://example.com/users/10?showAge=true'); |
| 23 | +``` |
| 24 | + |
| 25 | +### `replaceWith(routeNameOrPath, [params[, query]])` |
| 26 | + |
| 27 | +Programmatically replace current route with a new route. Does not add an |
| 28 | +entry into the browser history. |
| 29 | + |
| 30 | +#### Examples |
| 31 | + |
| 32 | +```js |
| 33 | +this.context.router.replaceWith('user', {id: 10}, {showAge: true}); |
| 34 | +this.context.router.replaceWith('about'); |
| 35 | +this.context.router.replaceWith('/users/10?showAge=true'); |
| 36 | +``` |
| 37 | + |
| 38 | +### `goBack()` |
| 39 | + |
| 40 | +Programmatically go back to the last route and remove the most recent |
| 41 | +entry from the browser history. Returns `true` unless it's the first entry |
| 42 | +in the app history. |
| 43 | + |
| 44 | +#### Example |
| 45 | + |
| 46 | +```js |
| 47 | +this.context.router.goBack(); |
| 48 | +``` |
| 49 | + |
| 50 | +If you want to make sure there was a history entry to go back to, use the return value: |
| 51 | + |
| 52 | +```js |
| 53 | +if (!this.context.router.goBack()) { |
| 54 | + this.context.router.transitionTo('/otherpage') |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### `makePath(routeName, params, query)` |
| 59 | + |
| 60 | +Creates a URL path to a route. |
| 61 | + |
| 62 | +### `makeHref(routeName, params, query)` |
| 63 | + |
| 64 | +Creates an `href` to a route. |
| 65 | + |
| 66 | +#### Example |
| 67 | + |
| 68 | +```js |
| 69 | +// given a route like this: |
| 70 | +// <Route name="user" path="users/:userId"/> |
| 71 | +this.context.router.makeHref('user', {userId: 123}); // "users/123" |
| 72 | +``` |
| 73 | + |
| 74 | +### `getPath()` |
| 75 | + |
| 76 | +Returns the current URL path, including query string. |
| 77 | + |
| 78 | +### `getPathname()` |
| 79 | + |
| 80 | +Returns the current URL path without the query string. |
| 81 | + |
| 82 | +### `getParams()` |
| 83 | + |
| 84 | +Returns a hash of the currently active URL params. |
| 85 | + |
| 86 | +### `getQuery()` |
| 87 | + |
| 88 | +Returns a hash of the currently active query params. |
| 89 | + |
| 90 | +### `isActive(routeName, params, query)` |
| 91 | + |
| 92 | +Returns `true` if a route, params, and query are active, `false` |
| 93 | +otherwise. |
| 94 | + |
| 95 | +### `getRoutes()` |
| 96 | + |
| 97 | +Returns an array of the currently active routes, in nesting order. |
| 98 | + |
| 99 | +Examples |
| 100 | +-------- |
| 101 | + |
| 102 | +Often you'll want access to params and query: |
| 103 | + |
| 104 | +```js |
| 105 | +// route |
| 106 | +<Route name="user" path="user/:name" handler={User} /> |
| 107 | + |
| 108 | +// handler |
| 109 | +var User = React.createClass({ |
| 110 | + render: function () { |
| 111 | + var name = this.context.router.getParams().name; |
| 112 | + return ( |
| 113 | + <div> |
| 114 | + <h1>{name}</h1> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +Let's say you are using bootstrap and want to get `active` on those `li` |
| 122 | +tags for the Tabs: |
| 123 | + |
| 124 | +```js |
| 125 | +var Link = require('react-router').Link; |
| 126 | + |
| 127 | +var Tab = React.createClass({ |
| 128 | + render: function () { |
| 129 | + var { router } = this.context; |
| 130 | + var isActive = router.isActive(this.props.to, this.props.params, this.props.query); |
| 131 | + var className = isActive ? 'active' : ''; |
| 132 | + var link = ( |
| 133 | + <Link {...this.props} /> |
| 134 | + ); |
| 135 | + return <li className={className}>{link}</li>; |
| 136 | + } |
| 137 | + |
| 138 | +}); |
| 139 | + |
| 140 | +// use it just like <Link/>, and you'll get an anchor wrapped in an `li` |
| 141 | +// with an automatic `active` class on both. |
| 142 | +<Tab to="foo">Foo</Tab> |
| 143 | +``` |
| 144 | + |
| 145 | +Some navigation: |
| 146 | + |
| 147 | +```js |
| 148 | +React.createClass({ |
| 149 | + render: function() { |
| 150 | + var { router } = this.context; |
| 151 | + return ( |
| 152 | + <div onClick={() => router.transitionTo('foo')}>Go to foo</div> |
| 153 | + <div onClick={() => router.replaceWith('bar')}>Go to bar without creating a new history entry</div> |
| 154 | + <div onClick={() => router.goBack()}>Go back</div> |
| 155 | + ); |
| 156 | + } |
| 157 | +}); |
| 158 | +``` |
| 159 | + |
| 160 | + |
0 commit comments