From c6301abaed51ef7ac2aa370ed18d66e39ca9e15d Mon Sep 17 00:00:00 2001 From: Nicolas Beck Date: Sat, 3 Mar 2018 01:06:27 +0100 Subject: [PATCH] Add initialRouteKey for StackRouter (#3540) * use initialRouteName as key when initializing StackRouter * fix null headerLeft * merge back * fixed tests * use config flag * fixed snapshots * implemented requested changes --- flow/react-navigation.js | 1 + src/routers/StackRouter.js | 3 ++- src/routers/__tests__/StackRouter-test.js | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/flow/react-navigation.js b/flow/react-navigation.js index df4e716..4354d54 100644 --- a/flow/react-navigation.js +++ b/flow/react-navigation.js @@ -362,6 +362,7 @@ declare module 'react-navigation' { initialRouteParams?: NavigationParams, paths?: NavigationPathsConfig, navigationOptions?: NavigationScreenConfig<*>, + initialRouteKey?: string, |}; declare export type NavigationStackViewConfig = {| diff --git a/src/routers/StackRouter.js b/src/routers/StackRouter.js index d720f3b..9d86ca5 100644 --- a/src/routers/StackRouter.js +++ b/src/routers/StackRouter.js @@ -91,11 +91,12 @@ export default (routeConfigs, stackConfig = {}) => { ...(action.params || {}), ...(initialRouteParams || {}), }; + const { initialRouteKey } = stackConfig; route = { ...route, ...(params ? { params } : {}), routeName: initialRouteName, - key: action.key || generateKey(), + key: action.key || (initialRouteKey || generateKey()), }; return { key: 'StackRouterRoot', diff --git a/src/routers/__tests__/StackRouter-test.js b/src/routers/__tests__/StackRouter-test.js index 5211a87..9fcc675 100644 --- a/src/routers/__tests__/StackRouter-test.js +++ b/src/routers/__tests__/StackRouter-test.js @@ -576,6 +576,23 @@ describe('StackRouter', () => { expect(state2.routes[1].routes[1].routes[1].routeName).toEqual('Corge'); }); + test('Navigate to initial screen is possible', () => { + const TestRouter = StackRouter( + { + foo: { screen: () =>
}, + bar: { screen: () =>
}, + }, + { initialRouteKey: 'foo' } + ); + const initState = TestRouter.getStateForAction(NavigationActions.init()); + const pushedState = TestRouter.getStateForAction( + NavigationActions.navigate({ routeName: 'foo', key: 'foo' }), + initState + ); + expect(pushedState.index).toEqual(0); + expect(pushedState.routes[0].routeName).toEqual('foo'); + }); + test('Navigate with key is idempotent', () => { const TestRouter = StackRouter({ foo: { screen: () =>
},