Aru*_*mar 4 react-native react-navigation
我在 React Native 应用程序中使用 React-navigation v2 创建了一些选项卡。我创建了 componentDidMount 函数,其中调用 willFocus。第一次运行应用程序并且第一次选择我想要的选项卡时,willFocus 不会执行。但是,当我转到不同的选项卡并返回该选项卡时,现在 willFocus 会执行。willFocus第一次不执行而第二次运行正常的原因是什么?
所需的选项卡 componentDidMount 函数
componentDidMount(){
const {navigation} = this.props;
navigation.addListener ('willFocus', async () =>{
console.log("willFocus runs"
});
}Run Code Online (Sandbox Code Playgroud)
选项卡导航器与其他导航器嵌套,但我只显示下面的选项卡导航器
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
}
},
}
),Run Code Online (Sandbox Code Playgroud)
我也遇到了这个问题,它被报告为反应导航的错误。查看此问题了解详细信息。
我有两个建议来解决这个问题。
PS:再次检查您的函数并确保调用该函数两次不会导致任何副作用
componentDidMount(){
console.log("willFocus runs") // calling it here to make sure it is logged at initial start
const {navigation} = this.props;
navigation.addListener ('willFocus', async () =>{
console.log("willFocus runs") // calling it here to make sure it is logged at every time screen is focused after initial start
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12528 次 |
| 最近记录: |