[英]React Native passing parameters through Navigator
I am new to the React Native framework and I have the following issue... 我是React Native框架的新手,我有以下问题......
I have a page that contains a Navigator and 2 text inputs for 'username' and 'password'. 我有一个页面,其中包含导航器和2个用于“用户名”和“密码”的文本输入。
I want to use this for a login, and at this step, just pass the parameters into the next page, and output to the screen "Hello {username}". 我想用它来登录,在这一步,只需将参数传递到下一页,然后输出到“Hello {username}”屏幕。
The code: 编码:
In FirstPage.js 在FirstPage.js
class FistPage extends Component {
constructor(props) {
super(props);
console.log('constructor');
this.state = {
username: '',
password: ''
}
}
gotoNext() {
if (this.state.username == '' || this.state.password == '') {
Alert.alert(
'Empty field(s)!',
'Please fill username and password!',
[
{text: 'OK', onPress: () => console.log('OK Pressed')},
]
)
} else {
this.props.navigator.push({
id: 'SecondPage',
name: 'Second Page',
passProps: {
username: this.state.username,
password: this.state.password,
}
});
}
}
I found that I can use passProps
: 我发现我可以使用
passProps
:
In SecondPage.js: 在SecondPage.js中:
class SecondPage extends Component {
constructor(props) {
super(props);
console.log('EmployeeManager - MainPage constructor');
this.username = props.username;
this.password = props.password;
}
renderScene(route, navigator) {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent:'center'}}>
<Text> Hello </Text>
<TouchableHighlight style={{backgroundColor: 'yellow', padding: 10}}
onPress={this.gotoPersonPage.bind(this)}>
<Text style={{ color: 'black'}}>Hello {this.username}</Text>
</TouchableHighlight>
</View>
);
}
this.username
is empty. this.username
为空。
How can I get the username passed from first page? 如何从第一页获取用户名?
Thanks in advance! 提前致谢!
EDIT. 编辑。 renderScene from FirstPage.js
来自FirstPage.js的renderScene
renderScene(route, navigator) {
return (
<View style={{flex: 1, alignItems: 'center', marginTop: 90}}>
<Text style={{textAlign: 'center', fontSize: 20}}>Welcome to EmployeeManager! </Text>
<Image source={require('./img/employees.png')} style={{width: 193, height: 130, marginTop: 10}}/>
<TextInput placeholder='Enter username...'
style={{width:200, textAlign: 'center'}}
onChangeText={(text) => this.setState({...this.state, username: text})}/>
<TextInput placeholder='Enter password...'
style={{width:200, textAlign: 'center'}}
secureTextEntry={true}
onChangeText={(text) => this.setState({...this.state, password: text})}/>
<TouchableHighlight
onPress={this.state.logged? this.exit.bind(this) : this.gotoNext.bind(this)}>
<Image source= {this.state.logged? require('./exitButton.png') : require('./loginButton.png')} style={{marginTop: 10}}/>
</TouchableHighlight>
); );
Can you show me your renderScene
? 你能告诉我你的
renderScene
吗? the one that is using Navigator in FirstPage
. 在
FirstPage
使用Navigator的那个。
Your renderScene
would look like this: 你的
renderScene
看起来像这样:
renderScene(route,navigator){
if ( route.id == 'SecondPage' ){
return (<SecondPage
username={route.passProps.username}
password={route.passProps.password} />);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.