Skip to content

Commit 08dc241

Browse files
nhunzakerryanflorence
authored andcommitted
[fix] Context warning with a ContextWrapper component
1 parent ba1e17a commit 08dc241

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This component is necessary to get around a context warning
3+
* present in React 0.13.0. It sovles this by providing a separation
4+
* between the "owner" and "parent" contexts.
5+
*/
6+
7+
var React = require('react');
8+
var PropTypes = require('../PropTypes');
9+
10+
class ContextWrapper extends React.Component {
11+
12+
getChildContext() {
13+
return {
14+
router: this.context.router,
15+
routeDepth: this.context.routeDepth
16+
};
17+
}
18+
19+
render() {
20+
return this.props.children;
21+
}
22+
}
23+
24+
ContextWrapper.contextTypes = {
25+
routeDepth: PropTypes.number.isRequired,
26+
router: PropTypes.router.isRequired
27+
};
28+
29+
ContextWrapper.childContextTypes = {
30+
routeDepth: PropTypes.number.isRequired,
31+
router: PropTypes.router.isRequired
32+
};
33+
34+
module.exports = ContextWrapper;

modules/components/RouteHandler.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var React = require('react');
2+
var ContextWrapper = require('./ContextWrapper')
23
var assign = require('react/lib/Object.assign');
34
var PropTypes = require('../PropTypes');
45

@@ -42,10 +43,7 @@ class RouteHandler extends React.Component {
4243
}
4344

4445
render() {
45-
// IMPORTANT: This span "soaks" up owner context, keeping
46-
// React 0.13.0 from throwing a warning.
47-
// TODO: Why should this even be necessary?
48-
return <span>{ this.createChildRouteHandler() }</span>
46+
return <ContextWrapper>{ this.createChildRouteHandler() }</ContextWrapper>
4947
}
5048

5149
}

0 commit comments

Comments
 (0)