如何在右键单击导航抽屉时打开下一个屏幕的反应原生?

时间:2016-10-14 09:36:23

标签: javascript ios react-native drawer

我想在react-native.中右键单击导航抽屉打开新屏幕 在登录submit按钮上,我调用了以下代码:

Login.js

this.props.navigator.push({
          id: 'FirstScreen',
          title: 'First Screen',
          iconRight: 'map',
          rightPress: this.onPress, // method to redirect to second screen
          rightText: 'list',
          passProps: {
              api_token: this.state.login_token
            }
        });

//navigate to second screen
 onPress() {
  this.props.navigator.push({
        id: "SecondScreen",
        title: 'Second Screen',
  });
}

但我在这一行上收到错误。

this.props.navigator.push({
            id: "SecondScreen",
            title: 'Second Screen',
      });

获取这些错误:

  

未定义不是对象(评估' this.props.navigator')

     

未处理的JS异常:undefined不是对象(评估   ' this.props.navigator&#39)

App.js

  _renderScene(route, navigator) {
        navigator.navigate = this.navigate;
            switch (route.id) {
                case ‘Login’:
                    return ( <Login navigator={navigator} {...route.passProps} />);
                case ‘FirstScreen’:
                    return ( <FirstScreen navigator={navigator} {...route.passProps} />);
                case 'SecondScreen':
                    return ( <SecondScreen navigator={navigator} {...route.passProps} />);
            }
        }

Navigation.js

module.exports = function (scene) {
    var componentMap = {
        'Login': {
            title: 'Login',
            id: 'Login'
        },
        'FirstScreen': {
            title: 'First Screen',
            id: 'FirstScreen'
        },
        'SecondScreen': {
            title: 'Second Screen',
            id: 'SecondScreen'
        }
    }

    return componentMap[scene];
}

1 个答案:

答案 0 :(得分:1)

您只需在导航器中更改this.onPress方法,如下所示,并删除包含rightPress的行:

Login.js

this.props.navigator.push({
       id: 'FirstScreen',
          title: 'First Screen',
          iconRight: 'map',
          rightText: 'list',
          passProps: {
              api_token: this.state.login_token
            }
          });
          onPress: () => {
            this.props.navigator.push({
                id: "SecondScreen",
                title: 'Second Screen',
                iconRight: 'map',
                passProps: {
                    api_token: this.state.login_token,
                    username: this.state.username
                },

              });
           }
        });