Javascript 反应本机:按下按钮不工作

Javascript 反应本机:按下按钮不工作,javascript,react-native,navigation,Javascript,React Native,Navigation,按按钮导航不起作用。这是我的登录页。我想注销 在onPressLogout()上从主屏幕切换到登录屏幕 index.js class Profile extends Component{ static propTypes = { navigator: PropTypes.shape({ getCurrentRoutes: PropTypes.func, jumpTo: PropTypes.func, }), } onPressLogout() {

按按钮导航不起作用。这是我的登录页。我想注销 在onPressLogout()上从主屏幕切换到登录屏幕

index.js

class Profile extends Component{
static propTypes = {
    navigator: PropTypes.shape({
        getCurrentRoutes: PropTypes.func,
        jumpTo: PropTypes.func,
    }),
}
onPressLogout() {
    const routeStack = this.props.navigator.getCurrentRoutes();
    this.props.navigator.jumpTo(routeStack[0]);
}
render(){
  return (
         <Container>
             <View style={styles.container}>        
               <Header>
                     <Button 
                     style={styles.button}
                     onPress={() => this.onPressLogout()}
                     >
                     <Icon name="ios-power" />
                     </Button>
                     <Title>Logout</Title>
               </Header>
            </Container>
 );
}
onPressLogout = () => {
  const routeStack = this.props.navigator.getCurrentRoutes();
  this.props.navigator.jumpTo(routeStack[0]);
}
使用
.bind()
更改此
的上下文。或者,使用箭头函数

bind()

这需要一个构造函数

constructor() {
  super();
  this.onPressLogout = this.onPressLogout.bind(this);
}
箭头功能

class Profile extends Component{
static propTypes = {
    navigator: PropTypes.shape({
        getCurrentRoutes: PropTypes.func,
        jumpTo: PropTypes.func,
    }),
}
onPressLogout() {
    const routeStack = this.props.navigator.getCurrentRoutes();
    this.props.navigator.jumpTo(routeStack[0]);
}
render(){
  return (
         <Container>
             <View style={styles.container}>        
               <Header>
                     <Button 
                     style={styles.button}
                     onPress={() => this.onPressLogout()}
                     >
                     <Icon name="ios-power" />
                     </Button>
                     <Title>Logout</Title>
               </Header>
            </Container>
 );
}
onPressLogout = () => {
  const routeStack = this.props.navigator.getCurrentRoutes();
  this.props.navigator.jumpTo(routeStack[0]);
}
旁注:

您可以在这里进行ES6分解

onPressLogout = () => {
  const {
     navigator: {
       getCurrentRoutes,
       jumpTo
     }
  } = this.props;
  const routeStack = getCurrentRoutes();
  jumpTo(routeStack[0]);
}

我尝试了这两种方法,但仍然存在相同的错误
undefined不是一个对象:评估这个$props$navigator.getCurrentRoutes
您是否可以只使用console.log
this.props.navigator
并对结果进行注释?它在控制台中显示了一些错误。我试着提醒,但没有结果。