Skip to content

Commit 7390b65

Browse files
committed
doc updates to new API
1 parent fe5a98d commit 7390b65

File tree

5 files changed

+40
-60
lines changed

5 files changed

+40
-60
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ You can find the library on `window.ReactRouter`.
6363
```js
6464
import React from 'react'
6565
import { render } from 'react-dom'
66-
import { Router, Route, Link } from 'react-router'
66+
import { Router, Route, Link, browserHistory } from 'react-router'
6767

6868
const App = React.createClass({/*...*/})
6969
const About = React.createClass({/*...*/})
@@ -112,7 +112,7 @@ const User = React.createClass({
112112
// instead, all you really need is a single root route, you don't need to
113113
// colocate the entire config).
114114
render((
115-
<Router>
115+
<Router history={browserHistory}>
116116
<Route path="/" component={App}>
117117
<Route path="about" component={About}/>
118118
<Route path="users" component={Users}>

docs/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- [RouteContext](#routecontext-mixin) (Deprecated)
2424

2525
* [Utilities](#utilities)
26-
* [useRoutes](#useroutescreatehistory)
26+
* [useRoutes](#useroutescreatehistory) (Deprecated)
2727
* [match](#match-routes-location-options--cb)
2828
* [createRoutes](#createroutesroutes)
2929
* [PropTypes](#proptypes)

docs/Introduction.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ Let's refactor our app to use React Router.
105105
import React from 'react'
106106
import { render } from 'react-dom'
107107

108-
// First we import some components...
109-
import { Router, Route, Link } from 'react-router'
108+
// First we import some modules...
109+
import { Router, Route, Link, browserHistory } from 'react-router'
110110

111111
// Then we delete a bunch of code from App and
112112
// add some <Link> elements...
@@ -134,8 +134,9 @@ const App = React.createClass({
134134
// Finally, we render a <Router> with some <Route>s.
135135
// It does all the fancy routing stuff for us.
136136
render((
137-
<Router>
137+
<Router history={browserHistory}>
138138
<Route path="/" component={App}>
139+
<IndexRoute component={Home} />
139140
<Route path="about" component={About} />
140141
<Route path="inbox" component={Inbox} />
141142
</Route>
@@ -151,6 +152,7 @@ Internally, the router converts your `<Route>` element hierarchy to a [route con
151152
const routes = {
152153
path: '/',
153154
component: App,
155+
indexRoute: { component: Home },
154156
childRoutes: [
155157
{ path: 'about', component: About },
156158
{ path: 'inbox', component: Inbox },
@@ -187,6 +189,7 @@ const Inbox = React.createClass({
187189
render((
188190
<Router>
189191
<Route path="/" component={App}>
192+
<IndexRoute component={Home} />
190193
<Route path="about" component={About} />
191194
<Route path="inbox" component={Inbox}>
192195
{/* add some nested routes where we want the UI to nest */}
@@ -205,7 +208,7 @@ Now visits to URLs like `inbox/messages/Jkei3c32` will match the new route and b
205208
```
206209
<App>
207210
<Inbox>
208-
<Message params={ {id: 'Jkei3c32'} } />
211+
<Message params={{ id: 'Jkei3c32' }}/>
209212
</Inbox>
210213
</App>
211214
```
@@ -215,7 +218,7 @@ And visits to `/inbox` will build this:
215218
```
216219
<App>
217220
<Inbox>
218-
<InboxStats />
221+
<InboxStats/>
219222
</Inbox>
220223
</App>
221224
```
Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
# Navigating Outside of Components
22

3-
While route components get `this.props.history` and the `History` mixin
4-
provides `this.history`, many apps want to be able to navigate outside
5-
of their components.
6-
7-
Its pretty simple, just hang on to your history object:
8-
9-
You can have a module in your app somewhere that exports your history
10-
object.
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`.
114

125
```js
13-
// history.js
14-
import createBrowserHistory from 'history/lib/createBrowserHistory'
15-
export default createBrowserHistory()
6+
// your main file that renders a Router
7+
import { Router, browserHistory } from 'react-router'
8+
import routes from './app/routes'
9+
render(<Router history={browserHistory} routes={routes}/>, el)
1610
```
1711

18-
And then import it to render a `<Router>`:
19-
20-
```js
21-
// index.js
22-
import history from './history'
23-
render(<Router history={history} routes={routes}/>, el)
24-
```
25-
26-
And now you can use that history object anywhere in your app, maybe in a
27-
flux actions file
28-
2912
```js
30-
// actions.js
31-
import history from './history'
32-
history.replaceState(null, '/some/path')
13+
// somewhere like a redux/flux action file:
14+
import { browserHistory } from './react-router'
15+
browserHistory.push('/some/path')
3316
```

docs/guides/basics/Histories.md

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Histories
22

3-
React Router is built on [history](https://github.com/rackt/history).
3+
React Router is built with [history](https://github.com/rackt/history).
44
In a nutshell, a history knows how to listen to the browser's address
55
bar for changes and parses the URL into a `location` object that the
66
router can use to match routes and render the correct set of components.
@@ -9,50 +9,38 @@ There are three types of histories you'll come across most often, but
99
note that anyone can build a custom history implementation for
1010
consumption with React Router.
1111

12-
- [`createHashHistory`](#createhashhistory)
13-
- [`createBrowserHistory`](#createbrowserhistory)
12+
- [`hashHistory`](#hashhistory)
13+
- [`browserHistory`](#browserhistory)
1414
- [`createMemoryHistory`](#creatememoryhistory)
1515

16-
Get them from the history package:
16+
You import them from the React Router package:
1717

1818
```js
1919
// JavaScript module import
20-
import createBrowserHistory from 'history/lib/createBrowserHistory'
21-
// or CommonJS
22-
var createBrowserHistory = require('history/lib/createBrowserHistory')
23-
24-
const history = createBrowserHistory()
20+
import { browserHistory } from 'react-router'
2521
```
2622

2723
Then pass them into your `<Router>`:
2824

2925
```js
3026
render(
31-
<Router history={history} routes={routes} />,
27+
<Router history={browserHistory} routes={routes} />,
3228
document.getElementById('app')
3329
)
3430
```
3531

36-
### `createHashHistory`
37-
This is the default history you'll get if you don't specify a history at all (i.e. `<Router>{/* your routes */}</Router>`). It uses the hash (`#`) portion of the URL creating routes that look like `example.com/#/some/path`.
32+
### `hashHistory`
33+
Hash history uses the hash (`#`) portion of the URL, creating routes that look like `example.com/#/some/path`.
3834

39-
#### Should I use `createHashHistory`?
40-
Hash history is the default because it works without any setup on your server, and works in all evergreen browsers and IE8+. But, we don't recommend using it in production, every web app should aspire to use `createBrowserHistory`.
35+
#### Should I use `hashHistory`?
36+
Hash history works without configuring your server, so if you're just getting started, go ahead and use it. But, we don't recommend using it in production, every web app should aspire to use `browserHistory`.
4137

4238
#### What is that `?_k=ckuvup` junk in the URL?
43-
When a history transitions around your app with `pushState` or `replaceState`, it can store "location state" on the new location that doesn't show up in the URL, think of it a little bit like post data in an HTML form.
39+
When a history transitions around your app with `push` or `replace`, it can store "location state" that doesn't show up in the URL on the new location, think of it a little bit like post data in an HTML form.
4440

4541
The DOM API that hash history uses to transition around is simply `window.location.hash = newHash`, with no place to store location state. But, we want all histories to be able to use location state, so we shim it by creating a unique key for each location and then store that state in session storage. When the visitor clicks "back" and "forward" we now have a mechanism to restore the location state.
4642

47-
You can disable that feature (more [here](http://rackt.org/history/stable/HashHistoryCaveats.html)):
48-
```js
49-
// Opt-out of persistent state, not recommended.
50-
let history = createHashHistory({
51-
queryKey: false
52-
});
53-
```
54-
55-
### `createBrowserHistory`
43+
### `browserHistory`
5644
Browser history is the recommended history for browser application with React Router. It uses the [History](https://developer.mozilla.org/en-US/docs/Web/API/History) API built into the browser to manipulate the URL, creating real URLs that look like `example.com/some/path`.
5745

5846
#### Configuring Your Server
@@ -100,6 +88,12 @@ You might wonder why we don't fall back to hash history; the problem is that URL
10088
### `createMemoryHistory`
10189
Memory history doesn't manipulate or read from the address bar. This is how we implement server rendering. It's also useful for testing and other rendering environments (like React Native).
10290

91+
Its a bit different than the other two histories because you have to
92+
create one, it is this way to facilitate testing:
93+
94+
```js
95+
const history = createMemoryHistory(location)
96+
```
10397

10498
## Example implementation
10599

@@ -109,15 +103,15 @@ app, the client entry point would look like:
109103
```js
110104
import React from 'react'
111105
import { render } from 'react-dom'
112-
import createBrowserHistory from 'history/lib/createBrowserHistory'
113-
import { Router, Route, IndexRoute } from 'react-router'
106+
import { browserHistory, Router, Route, IndexRoute } from 'react-router'
107+
114108
import App from '../components/App'
115109
import Home from '../components/Home'
116110
import About from '../components/About'
117111
import Features from '../components/Features'
118112

119113
render(
120-
<Router history={createBrowserHistory()}>
114+
<Router history={browserHistory}>
121115
<Route path='/' component={App}>
122116
<IndexRoute component={Home} />
123117
<Route path='about' component={About} />

0 commit comments

Comments
 (0)