简体   繁体   English

在React Native中使用可重用组件后,数据未使用onDataChange更改

[英]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. 在这种情况下,这会将valueplaceholderNameonChangeText属性传递给实际组件。 You could this way also shorthand placeholderName to just use placeholder . 您也可以通过这种方式将placeholderName为只使用placeholder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React Native 中制作可重用的组件? - Make a reusable component in react native? 如何使用 React 循环可重用组件 - how to loop a reusable component using react 为可重用组件 React Native 设置 State 问题 - Set State Problem For Reusable Component React Native 如何在反应中使用材料 ui 或 mui 创建可重用的下拉组件? - How to create reusable dropdown component using material ui or mui in react? 当我有可重用的React组件,父组件或子组件(可重用组件)时,在哪里在React中获取数据 - Where to fetch data in React when I have a reusable react component, Parent or Child component (reusable component) 使用 React Hooks,我如何制作一个可重用的组件和父组件相互利用? - Using React Hooks, how do I make a reusable component and parent component that utilize each other? 使用 Redux 在 React 组件中更改 State - Changing the State Inside a React Component with using Redux 多行可调可重用React-Native TextInput组件的最小高度 - Minimal height of multiline adjustable reusable React-Native TextInput component 在 React Native Maps 的组件上使用方法 - Using a method on a component in React Native Maps React Native 中使用文本组件的牢不可破的行 - Unbreakable Line in React Native Using Text Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM