File tree Expand file tree Collapse file tree 5 files changed +39
-2
lines changed Expand file tree Collapse file tree 5 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 1
1
import { PropTypes } from 'react'
2
2
3
3
// Works around issues with context updates failing to propagate.
4
+ // Caveat: the context value is expected to never change its identity.
4
5
// https://github.com/facebook/react/issues/2517
5
6
// https://github.com/reactjs/react-router/issues/470
6
7
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import RouterContext from './RouterContext'
6
6
import { createRoutes } from './RouteUtils'
7
7
import { createRouterObject } from './RouterUtils'
8
8
import warning from './routerWarning'
9
+ import assign from 'object-assign'
9
10
10
11
const { func, object } = React . PropTypes
11
12
@@ -88,7 +89,9 @@ const Router = React.createClass({
88
89
if ( error ) {
89
90
this . handleError ( error )
90
91
} else {
91
- this . router = this . createRouterObject ( state )
92
+ // Keep the identity of this.router because of a caveat in ContextUtils:
93
+ // they only work if the object identity is preserved.
94
+ assign ( this . router , this . createRouterObject ( state ) )
92
95
this . setState ( state , this . props . onUpdate )
93
96
}
94
97
} )
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ describe('withRouter', function () {
11
11
class App extends Component {
12
12
render ( ) {
13
13
expect ( this . props . router ) . toExist ( )
14
- return < h1 > App </ h1 >
14
+ return < h1 > { this . props . router . location . pathname } </ h1 >
15
15
}
16
16
}
17
17
@@ -39,4 +39,34 @@ describe('withRouter', function () {
39
39
done ( )
40
40
} )
41
41
} )
42
+
43
+ it ( 'updates the context inside static containers' , function ( done ) {
44
+ const WrappedApp = withRouter ( App )
45
+
46
+ class StaticContainer extends Component {
47
+ shouldComponentUpdate ( ) {
48
+ return false
49
+ }
50
+
51
+ render ( ) {
52
+ return this . props . children
53
+ }
54
+ }
55
+
56
+ const history = createHistory ( '/' )
57
+
58
+ render ( (
59
+ < Router history = { history } >
60
+ < Route component = { StaticContainer } >
61
+ < Route path = "/" component = { WrappedApp } />
62
+ < Route path = "/hello" component = { WrappedApp } />
63
+ </ Route >
64
+ </ Router >
65
+ ) , node , function ( ) {
66
+ expect ( node . firstChild . textContent ) . toEqual ( '/' )
67
+ history . push ( '/hello' )
68
+ expect ( node . firstChild . textContent ) . toEqual ( '/hello' )
69
+ done ( )
70
+ } )
71
+ } )
42
72
} )
Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
import hoistStatics from 'hoist-non-react-statics'
3
+ import { ContextSubscriber } from './ContextUtils'
3
4
import { routerShape } from './PropTypes'
4
5
5
6
function getDisplayName ( WrappedComponent ) {
@@ -8,6 +9,7 @@ function getDisplayName(WrappedComponent) {
8
9
9
10
export default function withRouter ( WrappedComponent ) {
10
11
const WithRouter = React . createClass ( {
12
+ mixins : [ ContextSubscriber ( 'router' ) ] ,
11
13
contextTypes : { router : routerShape } ,
12
14
render ( ) {
13
15
return < WrappedComponent { ...this . props } router = { this . context . router } />
Original file line number Diff line number Diff line change 35
35
"history" : " ^2.0.1" ,
36
36
"hoist-non-react-statics" : " ^1.0.5" ,
37
37
"invariant" : " ^2.2.1" ,
38
+ "object-assign" : " ^4.1.0" ,
38
39
"warning" : " ^2.1.0"
39
40
},
40
41
"peerDependencies" : {
You can’t perform that action at this time.
0 commit comments