From 194fcbfedde3c2c89d5665803217af9aeb9ad885 Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Tue, 6 Feb 2018 14:12:59 -0800 Subject: [PATCH] Fix StackRouter Replace Key Behavior (#3450) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace should actually provide new keys on the replaced route, use ‘newKey’ on the action if you want to define the new route key --- src/NavigationActions.js | 1 + src/routers/StackRouter.js | 2 +- src/routers/__tests__/StackRouter-test.js | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/NavigationActions.js b/src/NavigationActions.js index 8f4fbd154b..21a2b4d239 100644 --- a/src/NavigationActions.js +++ b/src/NavigationActions.js @@ -83,6 +83,7 @@ const reset = createAction(RESET, payload => ({ const replace = createAction(REPLACE, payload => ({ type: REPLACE, key: payload.key, + newKey: payload.newKey, params: payload.params, action: payload.action, routeName: payload.routeName, diff --git a/src/routers/StackRouter.js b/src/routers/StackRouter.js index fb3cea5a54..b26de59f23 100644 --- a/src/routers/StackRouter.js +++ b/src/routers/StackRouter.js @@ -201,7 +201,7 @@ export default (routeConfigs, stackConfig = {}) => { params: action.params, // merge the child state in this order to allow params override ...childState, - key: action.key, + key: action.newKey || generateKey(), routeName: action.routeName, }; return { ...state, routes }; diff --git a/src/routers/__tests__/StackRouter-test.js b/src/routers/__tests__/StackRouter-test.js index 2697b7b43c..f36515de02 100644 --- a/src/routers/__tests__/StackRouter-test.js +++ b/src/routers/__tests__/StackRouter-test.js @@ -533,8 +533,21 @@ describe('StackRouter', () => { ); expect(replacedState.index).toEqual(0); expect(replacedState.routes.length).toEqual(1); + expect(replacedState.routes[0].key).not.toEqual(initState.routes[0].key); expect(replacedState.routes[0].routeName).toEqual('bar'); expect(replacedState.routes[0].params.meaning).toEqual(42); + const replacedState2 = TestRouter.getStateForAction( + NavigationActions.replace({ + routeName: 'bar', + key: initState.routes[0].key, + newKey: 'wow', + }), + initState + ); + expect(replacedState2.index).toEqual(0); + expect(replacedState2.routes.length).toEqual(1); + expect(replacedState2.routes[0].key).toEqual('wow'); + expect(replacedState2.routes[0].routeName).toEqual('bar'); }); test('Handles push transition logic with completion action', () => {