反应本机导航器推送未定义的崩溃第一条路线

react native navigator push undefined crash first route

index.ios.js

render: function() {
    return (
        <NavigatorIOS
            style={styles.navigationContainer}
            initialRoute={{
            title: "Stories",
            component: Stories,
            leftButtonTitle : 'blabla',
            rightButtonTitle : 'Profile',
            onLeftButtonPress: () => {
             // this.blabla()
            },
            onRightButtonPress: () => {
             this.goProfile();
            }
        }} />
    );
}

goProfile 函数

goProfile : function  () {
     this.props.navigator.push({
          title: 'Profile',
          component: profilePage,
          leftButtonTitle: 'Home',
      });
},

无法读取未定义的 属性 'push' 此对象道具在对象中只有 rootTag。 为什么在首页应用程序崩溃。

尝试添加 navigator 参考:

ref="navigator"

并推送到 refs 而不是 props:

goProfile : function  () {
     this.refs.navigator.push({
          title: 'Profile',
          component: profilePage,
          leftButtonTitle: 'Home',
      });
},

因此,您的 NavigatorIOS 应如下所示:

<NavigatorIOS
    ref="navigator"
    style={styles.navigationContainer}
    initialRoute={{
    title: "Stories",
    component: Stories,
    leftButtonTitle : 'blabla',
    rightButtonTitle : 'Profile',
    onLeftButtonPress: () => {
       // this.blabla()
    },
    onRightButtonPress: () => {
      this.goProfile();
    }
}} />