7
7
8
8
A complete routing library for React.
9
9
10
- [ View the docs] ( https://rackt.github.io/react-router )
10
+ React Router keeps the URL in sync with nested UI. It has a simple API
11
+ with powerful features like lazy code loading, dynamic route matching,
12
+ and location transition handling built right in. Make the URL your first
13
+ thought, not an after-thought.
11
14
12
- Important Notes
13
- ---------------
15
+ Docs
16
+ ----
14
17
15
- ### SemVer
16
-
17
- Before our ` 1.0 ` release, breaking API changes will cause a bump to
18
- ` 0.x ` . For example, ` 0.4.1 ` and ` 0.4.8 ` will have the same API, but
19
- ` 0.5.0 ` will have breaking changes.
20
-
21
- Please refer to the [ upgrade guide] ( /UPGRADE_GUIDE.md ) and
22
- [ changelog] ( /CHANGELOG.md ) when upgrading.
18
+ - [ Guides and API Docs] ( https://rackt.github.io/react-router )
19
+ - [ Upgrade Guide] ( /UPGRADE_GUIDE.md )
20
+ - [ Changelog] ( /CHANGELOG.md )
23
21
24
22
Installation
25
23
------------
26
24
25
+ ### npm + webpack/browserify
26
+
27
27
``` sh
28
28
npm install react-router
29
- # or
30
- bower install react-router
31
29
```
32
30
33
- This library is written with CommonJS modules. If you are using
34
- browserify, webpack, or similar, you can consume it like anything else
35
- installed from npm.
31
+ Then with a module bundler or webpack, use as you would anything else:
36
32
37
- There is also a global build available on bower, find the library on
38
- ` window.ReactRouter ` .
33
+ ``` js
34
+ // using an ES6 transpiler
35
+ import { Router , Route , Link } from ' react-router' ;
39
36
40
- The library is also available on the popular CDN [ cdnjs] ( https://cdnjs.com/libraries/react-router ) .
37
+ // not using and ES6 transpiler
38
+ var ReactRouter = require (' react-router' );
39
+ var Router = ReactRouter .Router ;
40
+ var Route = ReactRouter .Route ;
41
+ ```
41
42
42
- Features
43
- --------
43
+ There's also a ` dist/ ` folder containing a UMD version.
44
44
45
- - Nested views mapped to nested routes
46
- - Modular construction of route hierarchy
47
- - Sync and async transition hooks
48
- - Transition abort / redirect / retry
49
- - Dynamic segments
50
- - Query parameters
51
- - Links with automatic ` .active ` class when their route is active
52
- - Multiple root routes
53
- - Hash or HTML5 history (with fallback) URLs
54
- - Declarative Redirect routes
55
- - Declarative NotFound routes
56
- - Browser scroll behavior with transitions
45
+ ### bower + who knows what
57
46
58
- Check out the ` examples ` directory to see how simple previously complex UI
59
- and workflows are to create.
47
+ ``` sh
48
+ bower install react-router
49
+ ```
50
+
51
+ Find the UMD/global build in ` dist/ ` , and the library on
52
+ ` window.ReactRouter ` . Best of luck to you.
53
+
54
+ ### CDN
55
+
56
+ Available on cdnjs [ here] ( https://cdnjs.com/libraries/react-router ) .
60
57
61
58
What's it look like?
62
59
--------------------
63
60
64
61
``` js
65
- var routes = (
66
- < Route handler= {App} path= " /" >
67
- < DefaultRoute handler= {Home} / >
68
- < Route name= " about" handler= {About} / >
69
- < Route name= " users" handler= {Users}>
70
- < Route name= " recent-users" path= " recent" handler= {RecentUsers} / >
71
- < Route name= " user" path= " /user/:userId" handler= {User} / >
72
- < NotFoundRoute handler= {UserRouteNotFound}/ >
73
- < / Route>
74
- < NotFoundRoute handler= {NotFound}/ >
75
- < Redirect from= " company" to= " about" / >
76
- < / Route>
77
- );
78
-
79
- Router .run (routes, function (Handler ) {
80
- React .render (< Handler/ > , document .body );
62
+ import { Router , Route } from ' react-router' ;
63
+ import BrowserHistory from ' react-router/lib/BrowserHistory' ;
64
+
65
+ var App = React .createClass ({/* ...*/ });
66
+ var About = React .createClass ({/* ...*/ });
67
+ // etc.
68
+
69
+ var Users = React .createClass ({
70
+ render () {
71
+ return (
72
+ < div>
73
+ < h1> Users< / h1>
74
+ < div className= " master" >
75
+ < ul>
76
+ {/* use Link to route around the app */ }
77
+ {this .state .users .map (user => (
78
+ < li>< Link to= {` /users/${ users .id } ` }> {user .name }< / Link>< / li>
79
+ ))}
80
+ < / ul>
81
+ < / div>
82
+ < div className= " detail" >
83
+ {this .props .children }
84
+ < / div>
85
+ < / div>
86
+ );
87
+ }
81
88
});
82
89
83
- // Or, if you'd like to use the HTML5 history API for cleaner URLs:
84
-
85
- Router .run (routes, Router .HistoryLocation , function (Handler ) {
86
- React .render (< Handler/ > , document .body );
90
+ var User = React .createClass ({
91
+ componentDidMount () {
92
+ this .setState ({
93
+ // route components are rendered with useful information, like URL params
94
+ user: findUserById (this .props .params .userId )
95
+ });
96
+ },
97
+
98
+ render () {
99
+ return (
100
+ < div>
101
+ < h2> {this .state .user .name }< / h2>
102
+ {/* etc. */ }
103
+ < / div>
104
+ );
105
+ }
87
106
});
88
- ```
89
-
90
- See more in the [ overview guide] ( /docs/guides/overview.md ) .
91
-
92
- Benefits of this Approach
93
- -------------------------
94
-
95
- 1 . ** Incredible screen-creation productivity** - There is only one
96
- use-case when a user visits a route: render something. Every user
97
- interface has layers (or nesting) whether it's a simple navbar or
98
- multiple levels of master-detail. Coupling nested routes to these
99
- nested views gets rid of a ton of work for the developer to wire all
100
- of it together when the user switches routes. Adding new screens
101
- could not get faster.
102
107
103
- 2 . ** Immediate understanding of application structure** - When routes
104
- are declared in one place, developers can easily construct a mental
105
- image of the application. It's essentially a sitemap. There's not a
106
- better way to get so much information about your app this quickly.
107
-
108
- 3 . ** Code tractability** - When a developer gets a ticket to fix a bug
109
- at as specific url they simply 1) look at the route config, then 2)
110
- go find the handler for that route. Every entry point into your
111
- application is represented by these routes.
112
-
113
- 4 . ** URLs are your first thought, not an after-thought** - With React
114
- Router, you don't get UI on the page without configuring a url first.
115
- Fortunately, it's wildly productive this way, too.
116
-
117
- Related Modules
118
- ---------------
108
+ // Declarative route configuration (could also load this config lazily
109
+ // instead, all you reall need is a single root route, you don't need to
110
+ // colocate the entire config).
111
+ React .render ((
112
+ < Router history= {BrowserHistory}>
113
+ < Route path= " /" component= {App}>
114
+ < Route path= " about" component= {About}/ >
115
+ < Route path= " users" component= {Users} indexComponent= {RecentUsers}>
116
+ < Route path= " /user/:userId" component= {User}/ >
117
+ < / Route>
118
+ < Route path= " *" component= {NoMatch}/ >
119
+ < / Route>
120
+ < / Router>
121
+ ), document .body );
122
+ ```
119
123
120
- - [ rnr-constrained-route] ( https://github.com/bjyoungblood/rnr-constrained-route ) - validate paths
121
- and parameters on route handlers.
122
- - [ react-router-bootstrap] ( https://github.com/react-bootstrap/react-router-bootstrap ) - Integration with [ react-bootstrap] ( https://github.com/react-bootstrap/react-bootstrap ) components.
123
- - [ react-router-proxy-loader] ( https://github.com/odysseyscience/react-router-proxy-loader ) - A Webpack loader to dynamically load react-router components on-demand
124
+ See more in the [ overview guide] (/docs/00 Guides/0 Overview.md) and [ Advanced
125
+ Usage] (/doc/00 Guides/Advanced Usage.md)
124
126
125
127
Contributing
126
128
------------
@@ -130,6 +132,6 @@ Please see [CONTRIBUTING](CONTRIBUTING.md)
130
132
Thanks, Ember
131
133
-------------
132
134
133
- This library is highly inspired by the Ember.js routing API. In general,
134
- it's a translation of the Ember router api to React. Huge thanks to the
135
- Ember team for solving the hardest part already.
135
+ React Router was initially inspired by Ember's fantastic Router. Many
136
+ thanks to the Ember team.
137
+
0 commit comments