反应导航路线状态有关键和索引

时间:2017-11-11 12:07:39

标签: react-native redux react-navigation

我无法使用NavigationActions.init()或文档中提到的状态正确生成状态。 这就是我的导航器的定义方式:

const HomeNav = TabNavigator({
feed:{screen:Feed},
restaurantScreen:{screen:Restaurant},
},{
tabBarComponent:NavigationComponent,
tabBarPosition:'bottom',
tabBarOptions:{
    bottomNavigationOptions:{
        labelColor:'white',
        rippleColor:'white',
        tabs:{
            feed:{
                barBackgroundColor:'#37474F',
            },
            restaurantScreen:{
                barBackgroundColor:'#00796B',
            }
        }
    }
}
});
export const Navigator=StackNavigator(
{
    HomeScreen:{screen:Home},
    VerifyScreen:{screen:Verify},
    newUserScreen1:{screen:newUser1},
    newUserScreen2:{screen:newUser2},
    loginScreen:{screen:login},
    feedScreen:{screen:HomeNav},
},
{
    headerMode:'none',
},
);

这就是我设置初始状态的方式:

const initialState=Navigator.router.getStateForAction(
NavigationActions.init()
);

function navigate(screenName,state){
return Navigator.router.getStateForAction(NavigationActions.navigate({routeName:screenName},state));
}

出现以下错误: Expect nav state to have routes and index,{routeName:'feedScreen',key:...}导航到供稿标签导航器时。

1 个答案:

答案 0 :(得分:0)

在您的第二个代码段中,它应该是这样的:

const initialState=Navigator.router.getStateForAction(
NavigationActions.init()
);

function navigate(screenName,state){
return Navigator.router.getStateForAction(NavigationActions.navigate({routeName:screenName}),state); 
}
  • getStateForAction收到第二个“州”参数,而是将其传递给NavigationActions.navigate

  • NavigationActions.init()期望任何像{routeName:'screen1'}这样的对象作为初始化状态的参数。所以它应该是这样的:NavigationActions.init({routeName:'screen1'})