android - react 导航 : Missing prop at top level component

标签 android ios reactjs react-native react-navigation

我按以下方式设置了堆栈导航器

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'

});

如您所见,我在 header 中使用了一个组件 HeaderRight,其中包含一些图标 - 设置齿轮、配置文件等。我希望能够从这些图标的 TouchableOpacity onPress 方法进行导航。但该组件中缺少导航 Prop “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; }} />
    );
  }
}

我看不出这在我的例子中是如何工作的。有人可以帮忙吗?谢谢

巨大

最佳答案

header 属性可以是函数也可以是对象。当它是一个函数时,navigation对象作为第一个参数传入,然后可以作为prop传递给HeaderRight组件.

navigationOptions: {
            header: (navigation) => {
                return {
                  style: {
                    backgroundColor: Colors.bgBrand,
                    elevation: null,
                  },
                  titleStyle: {
                     color: Colors.lightest
                  },
                  right: (<HeaderRight
                             navigation={navigation}
                          />),
               };
           },
},

关于android - react 导航 : Missing prop at top level component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42899523/

相关文章:

java - 根据用户输入设置菜单项android

java - 遍历引用 Java 中的父节点的分层列表/树

android - 在 Android 5.0 Lollipop 上不支持 calendarViewShown

reactjs - React useEffect 钩子(Hook)没有清除间隔

javascript - 在 Reactjs 中使用 firebase 电话身份验证时出现 "firebaseApp.auth.RecaptchaVerifier is not a constructor"错误

java - 垂直回收 View (包括水平回收 View )无法正常工作

ios - 如何从终端或使用任何Monkeytalk命令打开iPhone应用程序?

php - 新手在使用 ASIHTTPRequest 上传文件时遇到问题

ios - 无法在 Swift 中替换字符串

javascript - 在 props 接口(interface)中访问 React 类型