Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include location state in LOCATION_CHANGE payload #302

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' } })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test key as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed location.key from the perimeter of this PR, as it seems to be a private API of the history package.


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

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

Expand Down