Skip to content

Commit 893cbf0

Browse files
committed
Preserve the identity of this.router to avoid setting this.context directly
1 parent bb3e2df commit 893cbf0

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

modules/ContextUtils.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PropTypes } from 'react'
22

33
// Works around issues with context updates failing to propagate.
4+
// Caveat: the context value is expected to never change its identity.
45
// https://github.com/facebook/react/issues/2517
56
// https://github.com/reactjs/react-router/issues/470
67

@@ -43,9 +44,8 @@ export function ContextProvider(name) {
4344
},
4445

4546
componentDidUpdate() {
46-
const nextValue = this.getChildContext()[name]
4747
this[listenersKey].forEach(listener =>
48-
listener(this[eventIndexKey], nextValue)
48+
listener(this[eventIndexKey])
4949
)
5050
},
5151

@@ -112,12 +112,8 @@ export function ContextSubscriber(name) {
112112
this[unsubscribeKey] = null
113113
},
114114

115-
[handleContextUpdateKey](eventIndex, nextValue) {
115+
[handleContextUpdateKey](eventIndex) {
116116
if (eventIndex !== this.state[lastRenderedEventIndexKey]) {
117-
if (this.context[name] !== nextValue) {
118-
// React uses a stale value so we update it manually.
119-
this.context[name] = nextValue
120-
}
121117
this.setState({ [lastRenderedEventIndexKey]: eventIndex })
122118
}
123119
}

modules/Router.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import RouterContext from './RouterContext'
66
import { createRoutes } from './RouteUtils'
77
import { createRouterObject } from './RouterUtils'
88
import warning from './routerWarning'
9+
import assign from 'object-assign'
910

1011
const { func, object } = React.PropTypes
1112

@@ -88,7 +89,9 @@ const Router = React.createClass({
8889
if (error) {
8990
this.handleError(error)
9091
} 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))
9295
this.setState(state, this.props.onUpdate)
9396
}
9497
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"history": "^2.0.1",
3636
"hoist-non-react-statics": "^1.0.5",
3737
"invariant": "^2.2.1",
38+
"object-assign": "^4.1.0",
3839
"warning": "^2.1.0"
3940
},
4041
"peerDependencies": {

0 commit comments

Comments
 (0)