导航无法按下按钮。这是我的登录页面。我想退出 从主屏幕到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>
);
}
并在routeStack中
const routeStack = [
{ name: 'Login', component: Login},
]
答案 0 :(得分:0)
使用.bind()
更改this
的上下文。或者,使用箭头功能。
<强> 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]);
}