Skip to content

Commit

Permalink
Include location state and key in LOCATION_CHANGE payload
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Apr 30, 2019
1 parent 37e90d7 commit fb002fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ConnectedRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@ const createConnectedRouter = (structure) => {
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
state: stateInStore,
key: keyInStore,
} = getLocation(store.getState())
// Extract history's location
const {
pathname: pathnameInHistory,
search: searchInHistory,
hash: hashInHistory,
state: stateInHistory,
key: keyInHistory,
} = history.location

// If we do time travelling, the location in store is changed but location in history is not changed
if (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore) {
if (
pathnameInHistory !== pathnameInStore
|| searchInHistory !== searchInStore
|| hashInHistory !== hashInStore
|| stateInStore !== stateInHistory
|| keyInStore !== keyInHistory
) {
this.inTimeTravelling = true
// Update history's location to match store's location
history.push({
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
state: stateInStore,
key: keyInStore
})
}
})
Expand Down
13 changes: 13 additions & 0 deletions test/ConnectedRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ describe('ConnectedRouter', () => {
expect(onLocationChangedSpy.mock.calls).toHaveLength(3)
})

it('supports location state and key', () => {
mount(
<Provider store={store}>
<ConnectedRouter {...props}>
<Route path="/" render={() => <div>Home</div>} />
</ConnectedRouter>
</Provider>
)
props.history.push({ pathname: '/new-location', state: { foo: 'bar' } })

expect(onLocationChangedSpy.mock.calls[1][0].state).toEqual({ foo: 'bar'})
})

it('only renders one time when mounted', () => {
let renderCount = 0

Expand Down

0 comments on commit fb002fb

Please sign in to comment.