From 0de40c64643f7460ae75c927cfb04cdd52073ddf Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Mon, 19 Mar 2018 16:46:57 -0400 Subject: [PATCH] StackRouter to return null on idempotent navigation (#3793) This new behavior indicates that the action has been handled, but the state has not changed. --- .../src/routers/StackRouter.js | 2 +- .../src/routers/__tests__/StackRouter-test.js | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/react-navigation/src/routers/StackRouter.js b/packages/react-navigation/src/routers/StackRouter.js index c9dbdee7..0745dd90 100644 --- a/packages/react-navigation/src/routers/StackRouter.js +++ b/packages/react-navigation/src/routers/StackRouter.js @@ -209,7 +209,7 @@ export default (routeConfigs, stackConfig = {}) => { if (action.type !== NavigationActions.PUSH && lastRouteIndex !== -1) { // If index is unchanged and params are not being set, leave state identity intact if (state.index === lastRouteIndex && !action.params) { - return state; + return null; } // Remove the now unused routes at the tail of the routes array diff --git a/packages/react-navigation/src/routers/__tests__/StackRouter-test.js b/packages/react-navigation/src/routers/__tests__/StackRouter-test.js index 31606bbf..08c5e53d 100644 --- a/packages/react-navigation/src/routers/__tests__/StackRouter-test.js +++ b/packages/react-navigation/src/routers/__tests__/StackRouter-test.js @@ -539,8 +539,7 @@ describe('StackRouter', () => { NavigationActions.navigate({ routeName: 'bar' }), barState ); - expect(navigateOnBarState.index).toEqual(1); - expect(navigateOnBarState.routes[1].routeName).toEqual('bar'); + expect(navigateOnBarState).toEqual(null); }); test('Navigate focuses given routeName if already active in stack', () => { @@ -640,8 +639,7 @@ describe('StackRouter', () => { NavigationActions.navigate({ routeName: 'foo', key: 'foo' }), initState ); - expect(pushedState.index).toEqual(0); - expect(pushedState.routes[0].routeName).toEqual('foo'); + expect(pushedState).toEqual(null); }); test('Navigate with key is idempotent', () => { @@ -660,8 +658,20 @@ describe('StackRouter', () => { NavigationActions.navigate({ routeName: 'bar', key: 'a' }), pushedState ); - expect(pushedTwiceState.index).toEqual(1); - expect(pushedTwiceState.routes[1].routeName).toEqual('bar'); + expect(pushedTwiceState).toEqual(null); + }); + + test('Navigate to current routeName returns null to indicate handled action', () => { + const TestRouter = StackRouter({ + foo: { screen: () =>
}, + bar: { screen: () =>
}, + }); + const initState = TestRouter.getStateForAction(NavigationActions.init()); + const navigatedState = TestRouter.getStateForAction( + NavigationActions.navigate({ routeName: 'foo' }), + initState + ); + expect(navigatedState).toBe(null); }); test('Push behaves like navigate, except for key', () => {