[英]Data is not changing using onDataChange after using reusable component in react native
I created a reusable component Text input and I want to do a simple validation but i cant get the input value as it always showing null. 我创建了一个可重用的组件Text输入,我想做一个简单的验证,但由于总显示为空,所以我无法获得输入值。
This is where I am getting my username and password 这是我获取用户名和密码的地方
<LoginTextBox placeholderName='Email'
value={this.state.username}
onChangeText={(username) => this.setState({ username })}
></LoginTextBox>
<LoginTextBox placeholderName='Password'
value={this.state.password}
onChangeText={(password) => this.setState({ password })}
></LoginTextBox>
<TouchableOpacity
onPress={this.onLogin}>
This is where I created LoginTextBox 这是我创建LoginLogBox的地方
render() {
return (
<TextInput
placeholder={this.props.placeholderName}
style={styles.input}>{this.props.value}</TextInput>
);
}
Following is my validation 以下是我的验证
onLogin=()=> {
const { username, password } = this.state;
if (username.match("bhaskarj61@gmail.com")) {
this.props.navigation.navigate('NewsFeed');
}
else {
Alert.alert('enter valid email')
}
} }
onChangeText
-attribute never gets attached to your TextInput
-component. onChangeText
-attribute永远不会附加到您的TextInput
。
You could do something similar like this. 您可以执行类似的操作。
LoginTextBox -component render-method LoginTextBox-组件渲染方法
render(){
return(
<TextInput
{...this.props}
placeholder={this.props.placeholderName}
style={styles.input}>{this.props.value}</TextInput>
)
}
and here {...this.props}
on TextInput component passes your onChangeText
listener to actual React Native -component. 这里的TextInput组件上的
{...this.props}
将您的onChangeText
侦听器传递给实际的React Native组件。
In this case this would pass value
, placeholderName
and onChangeText
attributes to actual component. 在这种情况下,这会将
value
, placeholderName
和onChangeText
属性传递给实际组件。 You could this way also shorthand placeholderName
to just use placeholder
. 您也可以通过这种方式将
placeholderName
为只使用placeholder
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.