我想在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];
}
答案 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
},
});
}
});