You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
88
94
### Link
89
95
The primary way to allow users to navigate around your application. `<Link>` will render a fully accessible anchor tag with the proper href.
90
96
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.
92
98
93
99
#### Props
94
100
##### `to`
@@ -135,11 +141,50 @@ Given a route like `<Route path="/users/:userId" />`:
135
141
```
136
142
137
143
### IndexLink
138
-
Docs coming so soon!
144
+
Docs coming so soon! Please see the `active-links` example.
139
145
140
146
### 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`.
142
173
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.
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.
143
188
144
189
145
190
## Configuration Components
@@ -418,10 +463,9 @@ All the same props as [Redirect](#redirect) except for `from`.
418
463
## Route Components
419
464
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:
420
465
421
-
#### `history`
422
-
The Router's history [history](https://github.com/rackt/history/blob/master/docs).
466
+
### Injected Props
423
467
424
-
Useful mostly for transitioning around with `this.props.history.pushState(state, path, query)`
468
+
#### `history` (deprecated)
425
469
426
470
#### `location`
427
471
The current [location](https://github.com/rackt/history/blob/master/docs/Location.md).
@@ -512,173 +556,19 @@ class Users extends React.Component {
512
556
513
557
## Mixins
514
558
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`.
530
561
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 newhistory entry</div>
-[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.
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`.
669
564
565
+
## RouteContext Mixin [deprecated]
566
+
Deprecated, please see the `CHANGES.md`.
670
567
671
568
672
569
## Utilities
673
570
674
-
## `useRoutes(createHistory)`
675
-
Returns a new `createHistory` function that may be used to create history objects that know about routing.
Copy file name to clipboardExpand all lines: docs/guides/advanced/ConfirmingNavigation.md
+8-44Lines changed: 8 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,57 +1,21 @@
1
1
# Confirming Navigation
2
2
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.
9
4
10
5
```js
11
-
import { Lifecycle } from'react-router'
12
-
13
6
constHome=React.createClass({
14
7
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
22
10
},
23
11
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.
Copy file name to clipboardExpand all lines: docs/guides/advanced/NavigatingOutsideOfComponents.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Navigating Outside of Components
2
2
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`.
0 commit comments