Skip to content

Commit e56a117

Browse files
committed
more doc updates
1 parent 7390b65 commit e56a117

File tree

3 files changed

+73
-219
lines changed

3 files changed

+73
-219
lines changed

docs/API.md

Lines changed: 64 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
- [Link](#link)
66
- [IndexLink](#indexlink)
77
- [RouterContext](#routercontext)
8-
- RoutingContext (Deprecated, use `RouterContext`)
8+
- [context.router](#context-router)
9+
- RoutingContext (deprecated, use `RouterContext`)
910

1011
* [Configuration Components](#configuration-components)
1112
- [Route](#route)
@@ -18,12 +19,12 @@
1819
- [Named Components](#named-components)
1920

2021
* [Mixins](#mixins)
21-
- [Lifecycle](#lifecycle-mixin) (Deprecated)
22-
- [History](#history-mixin) (Deprecated)
23-
- [RouteContext](#routecontext-mixin) (Deprecated)
22+
- [Lifecycle](#lifecycle-mixin) (deprecated)
23+
- [History](#history-mixin) (deprecated)
24+
- [RouteContext](#routecontext-mixin) (deprecated)
2425

2526
* [Utilities](#utilities)
26-
* [useRoutes](#useroutescreatehistory) (Deprecated)
27+
* [useRoutes](#useroutescreatehistory) (deprecated)
2728
* [match](#match-routes-location-options--cb)
2829
* [createRoutes](#createroutesroutes)
2930
* [PropTypes](#proptypes)
@@ -42,7 +43,12 @@ One or many [`Routes`](#route) or [`PlainRoutes`](#plainroute). When the history
4243
Alias for `children`.
4344

4445
##### `history`
45-
The history the router should listen to from the `history` package.
46+
The history the router should listen to. Typically `browserHistory` or `hashHistory`.
47+
48+
```js
49+
import { browserHistory } from 'react-router'
50+
ReactDOM.render(<Router history={browserHistory}/>, el)
51+
```
4652

4753
##### `createElement(Component, props)`
4854
When the router is ready to render a branch of route components, it will use this function to create the elements. You may want to take control of creating the elements when you're using some sort of data abstraction, like setting up subscriptions to stores, or passing in some sort of application module to each component via props.
@@ -88,7 +94,7 @@ Please see the [`examples/`](/examples) directory of the repository for extensiv
8894
### Link
8995
The primary way to allow users to navigate around your application. `<Link>` will render a fully accessible anchor tag with the proper href.
9096

91-
A `<Link>` also knows when the route it links to is active and automatically applies its `activeClassName` and/or `activeStyle` when it is.
97+
A `<Link>` can know when the route it links to is active and automatically apply an `activeClassName` and/or `activeStyle` when given either prop.
9298

9399
#### Props
94100
##### `to`
@@ -135,11 +141,50 @@ Given a route like `<Route path="/users/:userId" />`:
135141
```
136142

137143
### IndexLink
138-
Docs coming so soon!
144+
Docs coming so soon! Please see the `active-links` example.
139145

140146
### RouterContext
141-
A `<RouterContext>` renders the component tree for a given router state and sets the history object and the current location in context.
147+
A `<RouterContext>` renders the component tree for a given router state. Its used by `Router` but also useful for server rendering and integrating in brownfield development.
148+
149+
It also provides a `router` object on `context`.
150+
151+
#### `context.router`
152+
153+
Contains data and methods relevant to routing. Most useful for imperatively transitioning around the application.
154+
155+
##### `push(pathnameOrLoc)`
156+
Transitions to a new URL, adding a new entry in the browser history.
157+
158+
```js
159+
router.push('/users/12')
160+
// or with location descriptor
161+
router.push({
162+
pathname: '/users/12',
163+
query: { modal: true },
164+
state: { fromDashboard: true }
165+
})
166+
```
167+
168+
##### `replace(pathnameOrLoc)`
169+
Identical to `push` except replaces the current history entry with a new one.
170+
171+
##### `go(n)`
172+
Go forward or backward in the history by `n` or `-n`.
142173

174+
##### `goBack()`
175+
Go back one entry in the history.
176+
177+
##### `goForward()`
178+
Go forward one entry in the history.
179+
180+
##### `createPath(pathname, query)`
181+
Stringifies the query into the pathname, using the router's config.
182+
183+
##### `createHref(pathname, query)`
184+
Creates a URL, using the router's config. For example, it will add `#/` in front of the `pathname` for hash history.
185+
186+
##### `isActive(pathnameOrLoc, onlyActiveOnIndex)`
187+
Returns `true` or `false` depending on if the `pathnameOrLoc` is active. Will be true for every route in the route branch matched (child route is active, therefore parent is too), unless `onlyActiveOnIndex` is specified, in which case it will only match the exact path.
143188

144189

145190
## Configuration Components
@@ -418,10 +463,9 @@ All the same props as [Redirect](#redirect) except for `from`.
418463
## Route Components
419464
A route's component is rendered when that route matches the URL. The router will inject the following properties into your component when it's rendered:
420465

421-
#### `history`
422-
The Router's history [history](https://github.com/rackt/history/blob/master/docs).
466+
### Injected Props
423467

424-
Useful mostly for transitioning around with `this.props.history.pushState(state, path, query)`
468+
#### `history` (deprecated)
425469

426470
#### `location`
427471
The current [location](https://github.com/rackt/history/blob/master/docs/Location.md).
@@ -512,173 +556,19 @@ class Users extends React.Component {
512556

513557
## Mixins
514558

515-
## Lifecycle Mixin
516-
Adds a hook to your component instance that is called when the router is about to navigate away from the route the component is configured on, with the opportunity to cancel the transition. Mostly useful for forms that are partially filled out.
517-
518-
On standard transitions, `routerWillLeave` receives a single argument: the `location` we're transitioning to. To cancel the transition, return false.
519-
520-
To prompt the user for confirmation, return a prompt message (string). `routerWillLeave` does not receive a location object during the beforeunload event in web browsers (assuming you're using the `useBeforeUnload` history enhancer). In this case, it is not possible for us to know the location we're transitioning to so `routerWillLeave` must return a prompt message to prevent the user from closing the tab.
521-
522-
#### Lifecycle Methods
523-
##### `routerWillLeave(nextLocation)`
524-
Called when the router is attempting to transition away from the route that rendered this component.
525-
526-
##### arguments
527-
- `nextLocation` - the next location
528-
529-
559+
## Lifecycle Mixin [deprecated]
560+
Deprecated, please see the `CHANGES.md`.
530561

531-
## History Mixin
532-
Adds the router's `history` object to your component instance.
533-
534-
**Note**: You do not need this mixin for route components, it's already available as `this.props.history`. This is for components deeper in the render tree that need access to the router's `history` object.
535-
536-
#### Methods
537-
##### `pushState(state, pathname, query)`
538-
Transitions to a new URL.
539-
540-
###### arguments
541-
- `state` - the location state.
542-
- `pathname` - the full URL with or without the query.
543-
- `query` - an object that will be stringified by the router.
544-
545-
##### `replaceState(state, pathname, query)`
546-
Replaces the current URL with a new one, without affecting the length of the history (like a redirect).
547-
548-
###### arguments
549-
- `state` - the location state.
550-
- `pathname` - the full URL with or without the query.
551-
- `query` - an object that will be stringified by the router.
552-
553-
##### `go(n)`
554-
Go forward or backward in the history by `n` or `-n`.
555-
556-
##### `goBack()`
557-
Go back one entry in the history.
558-
559-
##### `goForward()`
560-
Go forward one entry in the history.
561-
562-
##### `createPath(pathname, query)`
563-
Stringifies the query into the pathname, using the router's config.
564-
565-
##### `createHref(pathname, query)`
566-
Creates a URL, using the router's config. For example, it will add `#/` in front of the `pathname` for hash history.
567-
568-
##### `isActive(pathname, query, indexOnly)`
569-
Returns `true` or `false` depending on if the current path is active. Will be true for every route in the route branch matched by the `pathname` (child route is active, therefore parent is too), unless `indexOnly` is specified, in which case it will only match the exact path.
570-
571-
###### arguments
572-
- `pathname` - the full URL with or without the query.
573-
- `query` - if specified, an object containing key/value pairs that must be active in the current query - explicit `undefined` values require the corresponding key to be missing or `undefined` in the current query
574-
- `indexOnly` - a boolean (default: `false`).
575-
576-
#### Examples
577-
```js
578-
import { History } from 'react-router'
579-
580-
React.createClass({
581-
mixins: [ History ],
582-
render() {
583-
return (
584-
<div>
585-
<div onClick={() => this.history.pushState(null, '/foo')}>Go to foo</div>
586-
<div onClick={() => this.history.replaceState(null, 'bar')}>Go to bar without creating a new history entry</div>
587-
<div onClick={() => this.history.goBack()}>Go back</div>
588-
</div>
589-
)
590-
}
591-
})
592-
```
593-
594-
Let's say you are using bootstrap and want to get `active` on those `li` tags for the Tabs:
595-
596-
```js
597-
import { Link, History } from 'react-router'
598-
599-
const Tab = React.createClass({
600-
mixins: [ History ],
601-
render() {
602-
const isActive = this.history.isActive(this.props.to, this.props.query, this.props.onlyActiveOnIndex)
603-
const className = isActive ? 'active' : ''
604-
return <li className={className}><Link {...this.props}/></li>
605-
}
606-
})
607-
608-
// Use it just like <Link/>, and you'll get an anchor wrapped in an `li` with
609-
// an automatic `active` class on both when appropriate. Add `onlyActiveOnIndex`
610-
// to trigger <IndexLink/> behavior.
611-
<Tab to="/" onlyActiveOnIndex>Home</Tab>
612-
<Tab to="about">About</Tab>
613-
<Tab href="foo">Foo</Tab>
614-
```
615-
616-
#### But I’m using Classes
617-
> Notice how we never told you to use ES6 classes? :)
618-
619-
https://twitter.com/soprano/status/610867662797807617
620-
621-
If you aren't happy using `createClass` for a handful of components in your app for the sake of the `History` mixin, have a couple of options:
622-
623-
- Pass `this.props.history` from your route components down to the components that need it.
624-
- Use context
625-
626-
```js
627-
import { PropTypes } from 'react-router'
628-
629-
class MyComponent extends React.Component {
630-
doStuff() {
631-
this.context.history.pushState(null, '/some/path')
632-
}
633-
}
634-
635-
MyComponent.contextTypes = { history: PropTypes.history }
636-
```
637-
638-
- [Make your history a module](/docs/guides/advanced/NavigatingOutsideOfComponents.md)
639-
- Create a higher order component, we might end up shipping with this and deprecating history, just haven't had the time to think it through all the way.
640-
641-
```js
642-
function connectHistory(Component) {
643-
return React.createClass({
644-
mixins: [ History ],
645-
render() {
646-
return <Component {...this.props} history={this.history} />
647-
}
648-
})
649-
}
650-
651-
// other file
652-
import connectHistory from './connectHistory'
653-
654-
class MyComponent extends React.Component {
655-
doStuff() {
656-
this.props.history.pushState(null, '/some/where')
657-
}
658-
}
659-
660-
export default connectHistory(MyComponent)
661-
```
662-
663-
664-
665-
## RouteContext Mixin
666-
The RouteContext mixin provides a convenient way for route components to set the route in context. This is needed for routes that render elements that want to use the [Lifecycle mixin](#lifecycle) to prevent transitions.
667-
668-
It simply adds `this.context.route` to your component.
562+
## History Mixin [deprecated]
563+
Deprecated, please see the `CHANGES.md`.
669564

565+
## RouteContext Mixin [deprecated]
566+
Deprecated, please see the `CHANGES.md`.
670567

671568

672569
## Utilities
673570

674-
## `useRoutes(createHistory)`
675-
Returns a new `createHistory` function that may be used to create history objects that know about routing.
676-
677-
- listen((error, nextState) => {})
678-
- listenBeforeLeavingRoute(route, (nextLocation) => {})
679-
- match(location, (error, redirectLocation, nextState) => {})
680-
- isActive(pathname, query, indexOnly=false)
681-
571+
## `useRoutes(createHistory)` [deprecated]
682572

683573
## `match({ routes, location, ...options }, cb)`
684574

@@ -706,4 +596,4 @@ One or many [`Routes`](#route) or [`PlainRoutes`](#plainroute).
706596

707597

708598
## PropTypes
709-
Coming so soon!
599+
TODO (Pull Requests Welcome)

docs/guides/advanced/ConfirmingNavigation.md

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,21 @@
11
# Confirming Navigation
22

3-
React Router provides a [`routerWillLeave` lifecycle hook](/docs/Glossary.md#routehook) that React [component](/docs/Glossary.md#component)s may use to prevent a transition from happening or to prompt the user before leaving a [route](/docs/Glossary.md#route). [`routerWillLeave`](/docs/API.md#routerwillleavenextlocation) may either:
4-
5-
1. `return false` to cancel the transition or
6-
2. `return` a prompt message that will prompt the user for confirmation before leaving the route.
7-
8-
To install this hook, use the `Lifecycle` mixin in one of your [route component](/docs/Glossary.md#routecomponent)s.
3+
You can prevent a transition from happening or prompt the user before leaving a [route](/docs/Glossary.md#route) with a leave hook.
94

105
```js
11-
import { Lifecycle } from 'react-router'
12-
136
const Home = React.createClass({
147

15-
// Assuming Home is a route component, it may use the
16-
// Lifecycle mixin to get a routerWillLeave method.
17-
mixins: [ Lifecycle ],
18-
19-
routerWillLeave(nextLocation) {
20-
if (!this.state.isSaved)
21-
return 'Your work is not saved! Are you sure you want to leave?'
8+
contextTypes: {
9+
router: React.PropTypes.object
2210
},
2311

24-
// ...
25-
26-
})
27-
```
28-
29-
If you are using ES6 classes for your components, you can use [react-mixin](https://github.com/brigand/react-mixin) to add the `Lifecycle` mixin to your component, but we recommend using `React.createClass` for components that set up router lifecycle hooks.
30-
31-
If you need a [`routerWillLeave`](/docs/API.md#routerwillleavenextlocation) hook in a deeply nested component, simply use the [`RouteContext`](/docs/API.md#routecontext-mixin) mixin in your [route component](/docs/Glossary.md#routecomponent) to put the `route` in context.
32-
33-
```js
34-
import { Lifecycle, RouteContext } from 'react-router'
35-
36-
const Home = React.createClass({
37-
38-
// Home should provide its route in context
39-
// for descendants further down the hierarchy.
40-
mixins: [ RouteContext ],
41-
42-
render() {
43-
return <NestedForm />
44-
}
45-
46-
})
47-
48-
const NestedForm = React.createClass({
49-
50-
// Descendants use the Lifecycle mixin to get
51-
// a routerWillLeave method.
52-
mixins: [ Lifecycle ],
12+
componentDidMount() {
13+
this.context.router.setLeaveHook(this.props.route, this.routerWillLeave)
14+
},
5315

5416
routerWillLeave(nextLocation) {
17+
// return false to prevent a transition w/o prompting the user,
18+
// or return a string to allow the user to decide:
5519
if (!this.state.isSaved)
5620
return 'Your work is not saved! Are you sure you want to leave?'
5721
},

docs/guides/advanced/NavigatingOutsideOfComponents.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Navigating Outside of Components
22

3-
While route components get `this.props.router`, and `Router` puts on context `this.context.router` to navigate around, many apps want to be able to navigate outside of their components. They can do that with the history the app gives to `Router`.
3+
While you can use `this.context.router` to navigate around, many apps want to be able to navigate outside of their components. They can do that with the history the app gives to `Router`.
44

55
```js
66
// your main file that renders a Router

0 commit comments

Comments
 (0)