Skip to content

Commit

Permalink
Include location state in LOCATION_CHANGE payload (#302)
Browse files Browse the repository at this point in the history
* Include location state and key in LOCATION_CHANGE payload

Closes #301

* Remove location.key
  • Loading branch information
fzaninotto authored Jan 29, 2020
1 parent 7af0ed0 commit 08e2440
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/ConnectedRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,31 @@ const createConnectedRouter = (structure) => {
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
state: stateInStore,
} = getLocation(store.getState())
// Extract history's location
const {
pathname: pathnameInHistory,
search: searchInHistory,
hash: hashInHistory,
state: stateInHistory,
} = history.location

// If we do time travelling, the location in store is changed but location in history is not changed
if (props.history.action === 'PUSH' && (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore)) {
if (
props.history.action === 'PUSH' &&
(pathnameInHistory !== pathnameInStore ||
searchInHistory !== searchInStore ||
hashInHistory !== hashInStore ||
stateInStore !== stateInHistory)
) {
this.inTimeTravelling = true
// Update history's location to match store's location
history.push({
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
state: stateInStore,
})
}
})
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 08e2440

Please sign in to comment.