2017-08-07 83 views
1

導航不是按鈕press.This工作是我的登錄page.I想從主屏幕註銷 對onPressLogout()登錄屏幕陣營本地:按鈕onPress不工作

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> 
); 
} 

和routeStack

const routeStack = [ 
{ name: 'Login', component: Login}, 
] 

回答

0

要麼使用.bind()改變的背景。或者,使用箭頭功能。

bind()

這需要一個構造函數。

constructor() { 
    super(); 
    this.onPressLogout = this.onPressLogout.bind(this); 
} 

箭頭功能

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]); 
} 
+0

我已經試過這兩種方法,但還是同樣的錯誤'undefined是不是一個對象:評估_this $ props $ navigator.getCurrentRoutes' – anu

+0

你是否可以只用console.log this.props.navigator並返回結果? – Dan

+0

它在控制檯中顯示一些錯誤。我試圖提醒但沒有結果。 – anu