Skip to content

Commit 59aaaea

Browse files
committed
Moving most of RouteHandler component to a RouteHandler mixin
1 parent fefdb8c commit 59aaaea

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
examples/**/*-bundle.js
2+
node_modules

modules/components/RouteHandler.js

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var React = require('react');
2+
var RouteHandlerMixin = require('../mixins/RouteHandler');
23

34
/**
45
* A <RouteHandler> component renders the active child route handler
@@ -8,49 +9,16 @@ var RouteHandler = React.createClass({
89

910
displayName: 'RouteHandler',
1011

12+
mixins: [RouteHandlerMixin],
13+
1114
getDefaultProps: function () {
1215
return {
1316
ref: '__routeHandler__'
1417
};
1518
},
1619

17-
contextTypes: {
18-
getRouteAtDepth: React.PropTypes.func.isRequired,
19-
getRouteComponents: React.PropTypes.func.isRequired,
20-
routeHandlers: React.PropTypes.array.isRequired
21-
},
22-
23-
childContextTypes: {
24-
routeHandlers: React.PropTypes.array.isRequired
25-
},
26-
27-
getChildContext: function () {
28-
return {
29-
routeHandlers: this.context.routeHandlers.concat([ this ])
30-
};
31-
},
32-
33-
getRouteDepth: function () {
34-
return this.context.routeHandlers.length - 1;
35-
},
36-
37-
componentDidMount: function () {
38-
this._updateRouteComponent();
39-
},
40-
41-
componentDidUpdate: function () {
42-
this._updateRouteComponent();
43-
},
44-
45-
_updateRouteComponent: function () {
46-
var depth = this.getRouteDepth();
47-
var components = this.context.getRouteComponents();
48-
components[depth] = this.refs[this.props.ref];
49-
},
50-
5120
render: function () {
52-
var route = this.context.getRouteAtDepth(this.getRouteDepth());
53-
return route ? React.createElement(route.handler, this.props) : null;
21+
return this.getRouteHandler();
5422
}
5523

5624
});

modules/mixins/RouteHandler.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var React = require('react');
2+
3+
module.exports = {
4+
contextTypes: {
5+
getRouteAtDepth: React.PropTypes.func.isRequired,
6+
getRouteComponents: React.PropTypes.func.isRequired,
7+
routeHandlers: React.PropTypes.array.isRequired
8+
},
9+
10+
childContextTypes: {
11+
routeHandlers: React.PropTypes.array.isRequired
12+
},
13+
14+
getChildContext: function () {
15+
return {
16+
routeHandlers: this.context.routeHandlers.concat([ this ])
17+
};
18+
},
19+
20+
getRouteDepth: function () {
21+
return this.context.routeHandlers.length - 1;
22+
},
23+
24+
componentDidMount: function () {
25+
this._updateRouteComponent();
26+
},
27+
28+
componentDidUpdate: function () {
29+
this._updateRouteComponent();
30+
},
31+
32+
_updateRouteComponent: function () {
33+
var depth = this.getRouteDepth();
34+
var components = this.context.getRouteComponents();
35+
components[depth] = this.refs[this.props.ref || '__routeHandler__'];
36+
},
37+
38+
getRouteHandler(props) {
39+
var route = this.context.getRouteAtDepth(this.getRouteDepth());
40+
return route ? React.createElement(route.handler, props || this.props) : null;
41+
}
42+
};

0 commit comments

Comments
 (0)