Skip to content

Commit

Permalink
Preserve the identity of this.router to avoid setting this.context di…
Browse files Browse the repository at this point in the history
…rectly
  • Loading branch information
gaearon committed May 9, 2016
1 parent bb3e2df commit 893cbf0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
10 changes: 3 additions & 7 deletions modules/ContextUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PropTypes } from 'react'

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

Expand Down Expand Up @@ -43,9 +44,8 @@ export function ContextProvider(name) {
},

componentDidUpdate() {
const nextValue = this.getChildContext()[name]
this[listenersKey].forEach(listener =>
listener(this[eventIndexKey], nextValue)
listener(this[eventIndexKey])
)
},

Expand Down Expand Up @@ -112,12 +112,8 @@ export function ContextSubscriber(name) {
this[unsubscribeKey] = null
},

[handleContextUpdateKey](eventIndex, nextValue) {
[handleContextUpdateKey](eventIndex) {
if (eventIndex !== this.state[lastRenderedEventIndexKey]) {
if (this.context[name] !== nextValue) {
// React uses a stale value so we update it manually.
this.context[name] = nextValue
}
this.setState({ [lastRenderedEventIndexKey]: eventIndex })
}
}
Expand Down
5 changes: 4 additions & 1 deletion modules/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RouterContext from './RouterContext'
import { createRoutes } from './RouteUtils'
import { createRouterObject } from './RouterUtils'
import warning from './routerWarning'
import assign from 'object-assign'

const { func, object } = React.PropTypes

Expand Down Expand Up @@ -88,7 +89,9 @@ const Router = React.createClass({
if (error) {
this.handleError(error)
} else {
this.router = this.createRouterObject(state)
// Keep the identity of this.router because of a caveat in ContextUtils:
// they only work if the object identity is preserved.
assign(this.router, this.createRouterObject(state))
this.setState(state, this.props.onUpdate)
}
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"history": "^2.0.1",
"hoist-non-react-statics": "^1.0.5",
"invariant": "^2.2.1",
"object-assign": "^4.1.0",
"warning": "^2.1.0"
},
"peerDependencies": {
Expand Down

0 comments on commit 893cbf0

Please sign in to comment.