这是我的主屏幕
class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {username: null, password: null, isPasswordHidden: true, toggleText: 'Show'};
}
handleToggle = () => {
const { isPasswordHidden } = this.state;
if (isPasswordHidden) {
this.setState({isPasswordHidden: false});
this.setState({toggleText: 'Hide'});
} else {
this.setState({isPasswordHidden: true});
this.setState({toggleText: 'Show'});
}
}
//Validate() to check whether the input username is in Mail format
validate = (inputValue) => {
let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ; // Regex for Emails
// let reg = /^(\+\d{1,3}[- ]?)?\d{10}$/; // Regex for phone numbers
return reg.test(inputValue);
}
clearText(fieldName) {
this.refs[fieldName].clear(0);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}></Text>
<TextInput
ref={'input1'}
style={styles.input}
placeholder="Username"
onChangeText={value => this.setState({username: value})}
/>
<TextInput
ref={'input2'}
style={styles.input}
placeholder="Password"
maxLength={10}
secureTextEntry={this.state.isPasswordHidden}
onChangeText={value => this.setState({password: value})}
/>
<TouchableOpacity
onPress={this.handleToggle}
>
<Text>{this.state.toggleText}</Text>
</TouchableOpacity>
<View style={{padding: 20}}>
<TouchableOpacity onPress={() => {
if (!this.validate(this.state.username)) {
Alert.alert("Invalid");
Keyboard.dismiss();
} else if (this.state.username === 'vinay@gmail.com' && this.state.password === 'password') {
在这里,我已将参数传递到下一个屏幕,在该屏幕上要显示输入textInput字段的值。但是它不会在下一个屏幕中显示。我不知道为什么当我添加一个普通的堆栈导航器而没有任何抽屉导航时,一切正常。
this.props.navigation.navigate('Welcome', {
u_name: this.state.username,
p_word: this.state.password,
});
Keyboard.dismiss();
this.clearText('input1');
this.clearText('input2');
} else if (this.state.username === null && this.state.password === null) {
Alert.alert("Invalid");
} else {
Alert.alert("Login Failed");
this.clearText('input1');
this.clearText('input2');
Keyboard.dismiss();
}
}}>
<View style={styles.button}>
<Text style={styles.buttonText}>LOGIN</Text>
</View>
</TouchableOpacity>
</View>
</View>
);
}
}
这是我的欢迎屏幕
在这里,我已经从上一个屏幕中获取了值并将其存储到变量中。现在正在打印USERNAME:名称和PASSWORD:单词
class WelcomeScreen extends Component {
render() {
const { navigation } = this.props;
const u_name = navigation.getParam('username', 'name');
const p_word = navigation.getParam('password', 'word');
return (
<View style={styles.container}>
<Text style={styles.welcome}>WELCOME</Text>
<Text>USERNAME: {JSON.stringify(u_name)}</Text>
<Text>PASSWORD: {JSON.stringify(p_word)}</Text>
</View>
);
}
}
导航器
const WelcomeTabNavigator = createBottomTabNavigator({
Welcome: {screen: WelcomeScreen},
Profile,
Settings,
},
{
navigationOptions:({navigation}) => {
const {routeName} = navigation.state.routes[navigation.state.index];
return {
headerTitle: routeName
};
}
})
const WelcomeStackNavigator = createStackNavigator({
WelcomeTabNavigator: WelcomeTabNavigator
},
{
defaultNavigationOptions:({navigation}) => {
return {
headerLeft: (
<Icon
style={{paddingLeft: 20}}
onPress={() => navigation.openDrawer()}
name="md-menu"
size={30}
/>
)
};
}
})
const AppDrawerNavigator = createDrawerNavigator({
Welcome: {screen: WelcomeStackNavigator}
},
{
contentComponent:(props) => (
<View style={{flex:1}}>
<SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
<DrawerItems {...props} />
<Button
title="Logout"
onPress={() => {
props.navigation.navigate('Home')
}}
/>
</SafeAreaView>
</View>
),
drawerOpenRoute: 'DrawerOpen',
drawerCloseRoute: 'DrawerClose',
drawerToggleRoute: 'DrawerToggle'
})
const AppSwitchNavigator = createSwitchNavigator({
Home: {screen: HomeScreen},
Welcome: {screen: AppDrawerNavigator}
});
const AppContainer = createAppContainer(AppSwitchNavigator);
答案 0 :(得分:0)
我认为您应该在getParam
中使用由用户名插入的 u_name您的代码
const u_name = navigation.getParam('username', 'name');
新代码
const u_name = navigation.getParam('u_name', 'name');
密码相同
或者您可以使用这样的导航器方法而不更改getParam方法
this.props.navigation.navigate('Welcome', {
username: this.state.username,
password: this.state.password,
});
答案 1 :(得分:0)
我明白了。这是导航器部分的问题。我更改了
const AppSwitchNavigator = createSwitchNavigator({
Home: {screen: HomeScreen},
Welcome: {screen: AppDrawerNavigator}
});
对此
const AppSwitchNavigator = createStackNavigator({
Home: {screen: HomeScreen},
Welcome: {screen: AppDrawerNavigator}
});
添加开关导航器时,它将重置上一个屏幕,从而丢失输入值。