Skip to content

Commit e738c88

Browse files
committed
Use ES6 classes everywhere
1 parent a3d336f commit e738c88

17 files changed

+324
-323
lines changed

modules/IndexLink.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react'
22
import Link from './Link'
33

4-
const IndexLink = React.createClass({
4+
/**
5+
* An <IndexLink> is used to link to an <IndexRoute>.
6+
*/
7+
class IndexLink extends React.Component {
58

69
render() {
710
return <Link {...this.props} onlyActiveOnIndex={true} />
811
}
912

10-
})
13+
}
1114

1215
export default IndexLink

modules/IndexRoute.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,26 @@ const { bool, func } = React.PropTypes
1010
* An <IndexRoute> is used to specify its parent's <Route indexRoute> in
1111
* a JSX route config.
1212
*/
13-
const IndexRoute = React.createClass({
14-
15-
statics: {
16-
17-
createRouteFromReactElement(element, parentRoute) {
18-
if (parentRoute) {
19-
parentRoute.indexRoute = createRouteFromReactElement(element)
20-
} else {
21-
warning(
22-
false,
23-
'An <IndexRoute> does not make sense at the root of your route config'
24-
)
25-
}
13+
class IndexRoute extends React.Component {
14+
15+
static createRouteFromReactElement(element, parentRoute) {
16+
if (parentRoute) {
17+
parentRoute.indexRoute = createRouteFromReactElement(element)
18+
} else {
19+
warning(
20+
false,
21+
'An <IndexRoute> does not make sense at the root of your route config'
22+
)
2623
}
24+
}
2725

28-
},
29-
30-
propTypes: {
26+
static propTypes = {
3127
path: falsy,
3228
ignoreScrollBehavior: bool,
3329
component,
3430
components,
3531
getComponents: func
36-
},
32+
}
3733

3834
render() {
3935
invariant(
@@ -42,6 +38,6 @@ const IndexRoute = React.createClass({
4238
)
4339
}
4440

45-
})
41+
}
4642

4743
export default IndexRoute

modules/Link.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,27 @@ function isEmptyObject(object) {
3737
*
3838
* <Link ... query={{ show: true }} state={{ the: 'state' }} />
3939
*/
40-
const Link = React.createClass({
40+
class Link extends React.Component {
4141

42-
contextTypes: {
42+
static contextTypes = {
4343
history: object
44-
},
44+
}
4545

46-
propTypes: {
46+
static propTypes = {
4747
activeStyle: object,
4848
activeClassName: string,
4949
onlyActiveOnIndex: bool.isRequired,
5050
to: string.isRequired,
5151
query: object,
5252
state: object,
5353
onClick: func
54-
},
54+
}
5555

56-
getDefaultProps() {
57-
return {
58-
onlyActiveOnIndex: false,
59-
className: '',
60-
style: {}
61-
}
62-
},
56+
static defaultProps = {
57+
onlyActiveOnIndex: false,
58+
className: '',
59+
style: {}
60+
}
6361

6462
handleClick(event) {
6563
let allowTransition = true, clickResult
@@ -77,7 +75,7 @@ const Link = React.createClass({
7775

7876
if (allowTransition)
7977
this.context.history.pushState(this.props.state, this.props.to, this.props.query)
80-
},
78+
}
8179

8280
componentWillMount() {
8381
warning(
@@ -86,13 +84,13 @@ const Link = React.createClass({
8684
'some features including real hrefs, active styling, and navigation ' +
8785
'will not function correctly'
8886
)
89-
},
87+
}
9088

9189
render() {
9290
const { history } = this.context
9391
const { activeClassName, activeStyle, onlyActiveOnIndex, to, query, state, onClick, ...props } = this.props
9492

95-
props.onClick = this.handleClick
93+
props.onClick = (e) => this.handleClick(e)
9694

9795
// Ignore if rendered outside the context
9896
// of history, simplifies unit testing.
@@ -110,9 +108,9 @@ const Link = React.createClass({
110108
}
111109
}
112110

113-
return React.createElement('a', props)
111+
return <a {...props} />
114112
}
115113

116-
})
114+
}
117115

118116
export default Link

modules/Redirect.js

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,69 @@ import { falsy } from './PropTypes'
77
const { string, object } = React.PropTypes
88

99
/**
10-
* A <Redirect> is used to declare another URL path a client should be sent
11-
* to when they request a given URL.
10+
* A <Redirect> is used to declare another URL path a client should
11+
* be sent to when they request a given URL.
1212
*
13-
* Redirects are placed alongside routes in the route configuration and are
14-
* traversed in the same manner.
13+
* Redirects are placed alongside routes in the route configuration
14+
* and are traversed in the same manner.
1515
*/
16-
const Redirect = React.createClass({
17-
18-
statics: {
19-
20-
createRouteFromReactElement(element) {
21-
const route = createRouteFromReactElement(element)
22-
23-
if (route.from)
24-
route.path = route.from
25-
26-
route.onEnter = function (nextState, replaceState) {
27-
const { location, params } = nextState
28-
29-
let pathname
30-
if (route.to.charAt(0) === '/') {
31-
pathname = formatPattern(route.to, params)
32-
} else if (!route.to) {
33-
pathname = location.pathname
34-
} else {
35-
let routeIndex = nextState.routes.indexOf(route)
36-
let parentPattern = Redirect.getRoutePattern(nextState.routes, routeIndex - 1)
37-
let pattern = parentPattern.replace(/\/*$/, '/') + route.to
38-
pathname = formatPattern(pattern, params)
39-
}
40-
41-
replaceState(
42-
route.state || location.state,
43-
pathname,
44-
route.query || location.query
45-
)
16+
class Redirect extends React.Component {
17+
18+
static createRouteFromReactElement(element) {
19+
const route = createRouteFromReactElement(element)
20+
21+
if (route.from)
22+
route.path = route.from
23+
24+
route.onEnter = function (nextState, replaceState) {
25+
const { location, params } = nextState
26+
27+
let pathname
28+
if (route.to.charAt(0) === '/') {
29+
pathname = formatPattern(route.to, params)
30+
} else if (!route.to) {
31+
pathname = location.pathname
32+
} else {
33+
let routeIndex = nextState.routes.indexOf(route)
34+
let parentPattern = Redirect.getRoutePattern(nextState.routes, routeIndex - 1)
35+
let pattern = parentPattern.replace(/\/*$/, '/') + route.to
36+
pathname = formatPattern(pattern, params)
4637
}
4738

48-
return route
49-
},
39+
replaceState(
40+
route.state || location.state,
41+
pathname,
42+
route.query || location.query
43+
)
44+
}
5045

51-
getRoutePattern(routes, routeIndex) {
52-
let parentPattern = ''
46+
return route
47+
}
5348

54-
for (let i = routeIndex; i >= 0; i--) {
55-
let route = routes[i]
56-
let pattern = route.path || ''
57-
parentPattern = pattern.replace(/\/*$/, '/') + parentPattern
49+
static getRoutePattern(routes, routeIndex) {
50+
let parentPattern = ''
5851

59-
if (pattern.indexOf('/') === 0)
60-
break
61-
}
52+
for (let i = routeIndex; i >= 0; i--) {
53+
let route = routes[i]
54+
let pattern = route.path || ''
55+
parentPattern = pattern.replace(/\/*$/, '/') + parentPattern
6256

63-
return '/' + parentPattern
57+
if (pattern.indexOf('/') === 0)
58+
break
6459
}
6560

66-
},
61+
return '/' + parentPattern
62+
}
6763

68-
propTypes: {
64+
static propTypes = {
6965
path: string,
7066
from: string, // Alias for path
7167
to: string.isRequired,
7268
query: object,
7369
state: object,
7470
onEnter: falsy,
7571
children: falsy
76-
},
72+
}
7773

7874
render() {
7975
invariant(
@@ -82,6 +78,6 @@ const Redirect = React.createClass({
8278
)
8379
}
8480

85-
})
81+
}
8682

8783
export default Redirect

modules/Route.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,41 @@ import { component, components } from './PropTypes'
77
const { string, bool, func } = React.PropTypes
88

99
/**
10-
* A <Route> is used to declare which components are rendered to the page when
11-
* the URL matches a given pattern.
10+
* A <Route> is used to declare which components are rendered to the
11+
* page when the URL matches a given pattern.
1212
*
13-
* Routes are arranged in a nested tree structure. When a new URL is requested,
14-
* the tree is searched depth-first to find a route whose path matches the URL.
15-
* When one is found, all routes in the tree that lead to it are considered
16-
* "active" and their components are rendered into the DOM, nested in the same
17-
* order as they are in the tree.
13+
* Routes are arranged in a nested tree structure. When a new URL is
14+
* requested, the tree is searched depth-first to find a route whose
15+
* path matches the URL. When one is found, all routes in the tree
16+
* that lead to it are considered "active" and their components are
17+
* rendered into the DOM, nested in the same order as in the tree.
1818
*/
19-
const Route = React.createClass({
19+
class Route extends React.Component {
2020

21-
statics: {
21+
static createRouteFromReactElement(element) {
22+
const route = createRouteFromReactElement(element)
2223

23-
createRouteFromReactElement(element) {
24-
const route = createRouteFromReactElement(element)
24+
if (route.handler) {
25+
warning(
26+
false,
27+
'<Route handler> is deprecated, use <Route component> instead'
28+
)
2529

26-
if (route.handler) {
27-
warning(
28-
false,
29-
'<Route handler> is deprecated, use <Route component> instead'
30-
)
31-
32-
route.component = route.handler
33-
delete route.handler
34-
}
35-
36-
return route
30+
route.component = route.handler
31+
delete route.handler
3732
}
38-
39-
},
4033

41-
propTypes: {
34+
return route
35+
}
36+
37+
static propTypes = {
4238
path: string,
4339
ignoreScrollBehavior: bool,
4440
handler: component, // deprecated
4541
component,
4642
components,
4743
getComponents: func
48-
},
44+
}
4945

5046
render() {
5147
invariant(
@@ -54,6 +50,6 @@ const Route = React.createClass({
5450
)
5551
}
5652

57-
})
53+
}
5854

5955
export default Route

0 commit comments

Comments
 (0)