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
This commit is contained in:
Nicolas Beck 2018-03-03 01:06:27 +01:00 committed by Brent Vatne
parent 4569ad49f9
commit c6301abaed
3 changed files with 20 additions and 1 deletions

View File

@ -362,6 +362,7 @@ declare module 'react-navigation' {
initialRouteParams?: NavigationParams,
paths?: NavigationPathsConfig,
navigationOptions?: NavigationScreenConfig<*>,
initialRouteKey?: string,
|};
declare export type NavigationStackViewConfig = {|

View File

@ -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',

View File

@ -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: () => <div /> },
bar: { screen: () => <div /> },
},
{ 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: () => <div /> },