From 6c427c2a2aca13654b9f69ab2540a93f0a02e802 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Tue, 30 Apr 2019 12:50:02 +0200 Subject: [PATCH 1/2] Include location state and key in LOCATION_CHANGE payload Closes #301 --- src/ConnectedRouter.js | 15 ++++++++++++++- test/ConnectedRouter.test.js | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/ConnectedRouter.js b/src/ConnectedRouter.js index 14108879..e35fbf45 100644 --- a/src/ConnectedRouter.js +++ b/src/ConnectedRouter.js @@ -29,22 +29,35 @@ 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 (props.history.action === 'PUSH' && (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore)) { + if ( + props.history.action === 'PUSH' && + (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, }) } }) diff --git a/test/ConnectedRouter.test.js b/test/ConnectedRouter.test.js index af05a0b5..cdd4f65c 100644 --- a/test/ConnectedRouter.test.js +++ b/test/ConnectedRouter.test.js @@ -123,6 +123,19 @@ describe('ConnectedRouter', () => { expect(onLocationChangedSpy.mock.calls).toHaveLength(3) }) + it('supports location state and key', () => { + mount( + + +
Home
} /> +
+
+ ) + 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 From 912030ae9f7d6a45e7ccb570390ae50111bfa381 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Mon, 27 Jan 2020 11:13:09 +0100 Subject: [PATCH 2/2] Remove location.key --- src/ConnectedRouter.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ConnectedRouter.js b/src/ConnectedRouter.js index e35fbf45..bbf097c6 100644 --- a/src/ConnectedRouter.js +++ b/src/ConnectedRouter.js @@ -30,7 +30,6 @@ const createConnectedRouter = (structure) => { search: searchInStore, hash: hashInStore, state: stateInStore, - key: keyInStore, } = getLocation(store.getState()) // Extract history's location const { @@ -38,7 +37,6 @@ const createConnectedRouter = (structure) => { 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 @@ -47,8 +45,7 @@ const createConnectedRouter = (structure) => { (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore || - stateInStore !== stateInHistory || - keyInStore !== keyInHistory) + stateInStore !== stateInHistory) ) { this.inTimeTravelling = true // Update history's location to match store's location @@ -57,7 +54,6 @@ const createConnectedRouter = (structure) => { search: searchInStore, hash: hashInStore, state: stateInStore, - key: keyInStore, }) } })