RN开发中遇到的坑 - Can't find variable: navigate

RN开发中遇到的坑 - Can't find variable: navigate

测试RN开发的组件,在使用navigate做跳转的时候出现了红屏提示。

error
class loginView extends Component {
  static navigationOptions = {
    tabBarVisible: false, // 隐藏底部导航栏
    header:null,  //隐藏顶部导航栏
  };

  constructor(props){
      super(props);
      this.state = {title : '首界面'};
      this.btn_press = this.btn_press.bind(this);
    }

    render() {
    //注意这里已经添加但是仍然会红屏
    const {navigate} = this.props.navigation;
      return (
        
            
                  
                  
                  
                   this.btn_press()}
                        >
                        
                            登陆
                        
                  
                   {this.state.title}
            
      
    );
  }

  btn_press(){
  //点击事件
    console.log("onPressIn");
    ToastAndroid.show("A pikachu appeared nearby !", ToastAndroid.SHORT);
    navigate('Profile');
  }
}

loginView.defaultProps={
  appName : '测试应用'
};

const App = createStackNavigator({
  Home: { screen: loginView },
  Profile: { screen: Three },
});

在stackoverflow看了下别人的解决方案如下:
例如在代码

navigate('Profile');

跳转前添加下面代码

const {navigate} = this.props.navigation;

之后可能会有warning提示:

Warning: isMounted(...) is deprecated

添加以下代码

import {
  YellowBox,
} from 'react-native';

YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);

可消去提示


参考链接:https://stackoverflow.com/questions/43717456/cant-find-variable-navigate

你可能感兴趣的:(RN开发中遇到的坑 - Can't find variable: navigate)