【发布时间】:2019-06-25 16:00:11
【问题描述】:
我在 react native 应用程序中使用 react-navigation v2 创建了一些选项卡。我创建了调用 willFocus 的 componentDidMount 函数。第一次运行应用程序并且第一次选择了我想要的选项卡时,willFocus 没有执行。但是,当我转到不同的选项卡并返回该选项卡时,willFocus 将执行。 willFocus 第一次不执行而第二次正常工作的原因是什么?
所需的选项卡组件DidMount 函数
componentDidMount(){
const {navigation} = this.props;
navigation.addListener ('willFocus', async () =>{
console.log("willFocus runs"
});
}
标签导航器与其他导航器嵌套,但我只在下面显示标签导航器
TabNav: createBottomTabNavigator(
{
TAB1: Screen1,
TAB2: Screen2,
TAB3: Screen3,
TAB4: Screen4,
TAB5: Screen5,
// Map: MapScreen
},
{
initialRouteName: 'Bar',
transitionConfig: NavigationConfig,
navigationOptions: ({navigation})=>({
tabBarIcon: ({focused, tintColor})=>{
const { routeName } = navigation.state;
let iconName, iconSize;
switch(routeName) {
case "TAB1":
iconName = `icon1`;
break;
case "TAB2":
iconName = `icon2`;
break;
case "TAB3":
iconName = `icon3`;
break;
case "TAB4":
iconName = `icon4`;
break;
case "TAB5":
iconName = `icon5`;
break;
default:
break;
}
return <Icon name={iconName} color={tintColor} type="FontAwesome" />;
},
}),
tabBarOptions: {
activeTintColor: 'black',
inactiveTintColor: 'grey',
activeBackgroundColor: '#abaf9b',
labelStyle: {
fontSize: 12,
},
// style for tabbar
style: {
backgroundColor: '#ffffff',
height: 60,
justifyContent: 'space-around',
alignContent: 'center',
alignItems: 'center',
},
// style for tab
tabStyle: {
paddingTop: 7,
paddingBottom: 7
}
},
}
),
【问题讨论】:
标签: react-native react-navigation