Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit 2bbb3c7

Browse files
committed
Default params on stack router
1 parent b9389f3 commit 2bbb3c7

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

example/App.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { ListSection, Divider } from 'react-native-paper';
1010

1111
import SimpleStack from './src/SimpleStack';
12+
import SimpleTabs from './src/SimpleTabs';
1213

1314
// Comment the following two lines to stop using react-native-screens
1415
import { useScreens } from 'react-native-screens';
@@ -19,7 +20,8 @@ I18nManager.forceRTL(false);
1920
// useScreens();
2021

2122
const data = [
22-
{ component: SimpleStack, title: 'Simple', routeName: 'SimpleStack' },
23+
{ component: SimpleStack, title: 'Simple Stack', routeName: 'SimpleStack' },
24+
{ component: SimpleTabs, title: 'Simple Tabs', routeName: 'SimpleTabs' },
2325
];
2426

2527
// Cache images

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"react-native-paper": "2.0.0-alpha.4",
2020
"react-native-screens": "^1.0.0-alpha.9",
2121
"react-navigation-stack": "^1.0.0-alpha.23",
22-
"react-navigation-tabs": "^1.0.0-alpha.3"
22+
"react-navigation-tabs": "^1.0.0-alpha.4"
2323
},
2424
"devDependencies": {
2525
"babel-plugin-module-resolver": "^3.0.0",

example/src/SimpleTabs.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import { createBottomTabNavigator } from 'react-navigation-tabs';
3+
import { Button, Text, View } from 'react-native';
4+
import { Feather } from '@expo/vector-icons';
5+
6+
class Screen extends React.Component {
7+
static navigationOptions = ({ navigation }) => ({
8+
tabBarLabel: navigation.getParam('title'),
9+
tabBarIcon: ({ focused, tintColor }) => (
10+
<Feather size={25} name={navigation.getParam('icon')} color={tintColor} />
11+
),
12+
});
13+
14+
render() {
15+
return (
16+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
17+
<Text>{JSON.stringify(this.props.navigation.state.params)}</Text>
18+
<Button
19+
title="Go back"
20+
onPress={() => this.props.navigation.goBack(null)}
21+
/>
22+
</View>
23+
);
24+
}
25+
}
26+
27+
export default createBottomTabNavigator(
28+
{
29+
A: {
30+
screen: Screen,
31+
params: { title: 'First One', icon: 'activity' },
32+
},
33+
B: {
34+
screen: Screen,
35+
params: { title: 'Second One', icon: 'aperture' },
36+
},
37+
},
38+
{
39+
tabBarOptions: {
40+
activeTintColor: '#000',
41+
inactiveTintColor: '#eee',
42+
},
43+
}
44+
);

example/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,10 +4444,10 @@ react-navigation-stack@^1.0.0-alpha.23:
44444444
resolved "https://registry.yarnpkg.com/react-navigation-stack/-/react-navigation-stack-1.0.0-alpha.23.tgz#515e940b5e1f864a73a80be43cafae767f9ed3f5"
44454445
integrity sha512-EhM9SIlWYeCC1f1Ju53AUVfTyY6HLJwuGMVcb/VeVT513fPgOijD4HlmOcAngkKNVMNOfhPuFgvngg9TM4vYOA==
44464446

4447-
react-navigation-tabs@^1.0.0-alpha.3:
4448-
version "1.0.0-alpha.3"
4449-
resolved "https://registry.yarnpkg.com/react-navigation-tabs/-/react-navigation-tabs-1.0.0-alpha.3.tgz#dae59e0a448cfa8e194fe3776b1659e117f3a39b"
4450-
integrity sha512-V1iMy4QFhiWuBaaUMgWOuuBe0xl+/LXwiSwl4ejRDGRiPP7JLiqxFfgIo77X0Ce9YN2FOqq4A8GB7d4ssJtQIw==
4447+
react-navigation-tabs@^1.0.0-alpha.4:
4448+
version "1.0.0-alpha.4"
4449+
resolved "https://registry.yarnpkg.com/react-navigation-tabs/-/react-navigation-tabs-1.0.0-alpha.4.tgz#321c8cc19d14268d343a830689c741bb94c6ba80"
4450+
integrity sha512-ng8sCJmcQj1ciWaj0eJudQvYng/oL24konNPNudSrMyVKAfKr4+HcRPLY/rol+efLJ8UGjXGv2qdRNkBNeaLew==
44514451
dependencies:
44524452
hoist-non-react-statics "^2.5.0"
44534453
prop-types "^15.6.1"

src/routers/SwitchRouter.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ export default (routeConfigs, config = {}) => {
4343
}
4444
});
4545

46+
function getParamsForRoute(routeName, params) {
47+
let routeConfig = routeConfigs[routeName];
48+
if (routeConfig && routeConfig.params) {
49+
return { ...routeConfig.params, ...params };
50+
} else {
51+
return params;
52+
}
53+
}
54+
4655
const {
4756
getPathAndParamsForRoute,
4857
getActionForPathAndParams,
@@ -56,8 +65,12 @@ export default (routeConfigs, config = {}) => {
5665
}
5766

5867
function resetChildRoute(routeName) {
59-
const params =
68+
let initialParams =
6069
routeName === initialRouteName ? initialRouteParams : undefined;
70+
// note(brentvatne): merging initialRouteParams *on top* of default params
71+
// on the route seems incorrect but it's consistent with existing behavior
72+
// in stackrouter
73+
let params = getParamsForRoute(routeName, initialParams);
6174
const childRouter = childRouters[routeName];
6275
if (childRouter) {
6376
const childAction = NavigationActions.init();

0 commit comments

Comments
 (0)