我通过以下方式设置了堆栈导航器
const wishlizt = StackNavigator({
Splash: {screen: SplashScreen},
SignIn: {screen: SignInScreen},
SignUp: {screen: SignUpScreen},
Profile: {screen: ProfileScreen},
Home: {screen: MainScreenNavigator},
Chat: {screen: ChatScreen}
},
{
navigationOptions: {
title: 'Wishlizt',
header: {
style: {
backgroundColor: Colors.bgBrand,
elevation: null,
},
titleStyle: {
color: Colors.lightest
},
right: <HeaderRight />
}
},
initialRouteName: 'Splash'
});
如您所见,我在标题中使用了一个组件 HeaderRight,其中包含一些图标 - 设置 cog、配置文件等。我希望能够从这些图标的 TouchableOpacity onPress 方法中导航。但是该组件中缺少导航道具“this.props.navigation”。
官方文档页面有这个关于如何调用顶级组件导航的代码示例,并建议使用'ref'
const AppNavigator = StackNavigator(SomeAppRouteConfigs);
class App extends React.Component {
someEvent() {
// call navigate for AppNavigator here:
this.navigator && this.navigator.dispatch({ type: 'Navigate', routeName, params });
}
render() {
return (
<AppNavigator ref={nav => { this.navigator = nav; }} />
);
}
}
我无法在我的示例中看到它是如何工作的。有人可以帮忙吗?谢谢
巨大的