简体   繁体   English

当文本输入和提交按钮都在 React Native 中的不同 js 文件中时,如何在单击提交时进行空检查并导航到新屏幕?

[英]How to null check and navigate to new screen on click submit when textinput and submit button both are in different js files in react native?

render(){
  return (
    <ImageBackground
      source={require('../images/back02.png')}
      style={styles.bgscreen}
    >
    <KeyboardAvoidingView behavior='position'>
      <Image
        style={styles.headImage}
        source={require('../images/login_img.png')}
      />
      <ImgBack/>
      </KeyboardAvoidingView>
      <BottomButton navigation={this.props.navigation} />
    </ImageBackground>
  );}
}

ImgBack contains username and password textinput. ImgBack包含用户名和密码文本输入。 BottomButton contains the submit button BottomButton包含提交按钮

I want to navigate to new activity when submit button clicked.单击提交按钮时,我想导航到新活动。 navigation to new screen is working perfectly but before navigating i want to null check the TextInput which are on导航到新屏幕工作正常,但在导航之前,我想空检查打开的 TextInput

I am new to React-Native.我是 React-Native 的新手。 Complete Beginner here.在这里完成初学者。 I want even know what to do.我什至想知道该怎么做。 Help.帮助。

ImgBack.js file ImgBack.js 文件

class imgBack extends React.Component  { 

    constructor()
    {
      super();
      this.state = { hidePassword: true }
    }

    managePasswordVisibility = () =>
    {
      this.setState({ hidePassword: !this.state.hidePassword });
    }
    usernameValidate = (EnteredValue) =>{

        var TextLength = EnteredValue.length.toString();
        if(TextLength == 10 ){
          Alert.alert("Sorry, You have reached the maximum input limit.")
        }
        else if(TextLength == 0){
          Alert.alert("Username can't be blank")
        }
      }

      passValidate = (EnteredValue) =>{

        var TextLength = EnteredValue.length.toString();
        if(TextLength == 10 ){
          Alert.alert("Sorry, You have reached the maximum input limit.")
        }
        else if(TextLength == 0){
          Alert.alert("Username can't be blank")
        }
      }

    render(){
        return (
        <ImageBackground resizeMode='contain'
            source={require('../images/login_back.png')}
            style={{
                marginHorizontal: 10,
                height: 290,
                padding: 30,
            }}>

            <View style={
                styles.textInputContainer
            } >
                <TextInput
                    placeholder="Username"
                    underlineColorAndroid = "transparent"
                    placeholderTextColor="#000000"
                    maxLength={10}
                    onChangeText={ EnteredValue => this.usernameValidate(EnteredValue) }
                />
            </View>

            <View style = { styles.textInputContainer }>
                <TextInput
                onChangeText={ EnteredValue => this.passValidate(EnteredValue) }
                underlineColorAndroid = "transparent"
                secureTextEntry = { this.state.hidePassword }
                placeholder="Password"
                placeholderTextColor="#000"
                />
                    <TouchableOpacity style = { styles.visibilityBtn } onPress = { this.managePasswordVisibility }>
                        <Image source = { ( this.state.hidePassword ) ? require('../images/eye_close_icon.imageset/eye_close_icon.png') : require('../images/eye_icon.imageset/eye_icon.png') } style = { styles.btnImage } />
                    </TouchableOpacity>
            </View>
        </ImageBackground>
    )
}
} ```

**Bottombutton File**

class bottomButon extends Component {类底部按钮扩展组件{

render(){
    return (
    <ImageBackground
        style={{ height: 80, marginLeft: '20%', marginTop: 10 }}
        resizeMode={'center'}
        source={require('../images/btn_back.png')} >
        <TouchableOpacity
        onPress={ this.login }  >
            <Text style={{ textAlign: 'center', marginTop: 25 }}>Submit & SYNC</Text>
        </TouchableOpacity>
    </ImageBackground>
)

} } } }

export default bottomButon;导出默认底部按钮; ``` ``

solution:解决方案:

constructor(props) {
    super(props);
    this.state = {
      hidePassword: true,
      username: '',
      password: '',
    };
  }

  validUserPass = async () => {
    try {
      if (this.state.username === '') {
        Alert.alert('Username is required !');
      } else if (this.state.password === '') {
        Alert.alert('Password is required !');
      } else {
        this.props.navigation.navigate('selectEmoji');
      }
    } catch (error) {
      console.warn(error);
    }
  };

it helped me solve my issue.它帮助我解决了我的问题。

暂无
暂无

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

相关问题 在 React Native 中,当用户在 TextInput 中写入时,如何避免用户点击提交按钮两次? - In React Native, how can I avoid having a user click a submit button twice, when they are writing in a TextInput? 在TextInput提交上渲染-React Native - Render on TextInput Submit - React Native 如何使用react-navigation在react-native android应用程序中单击按钮时导航到不同的屏幕 - How to navigate to different screen on button click in react-native android app using react-navigation 在React Native中提交表单后如何导航? - How to navigate after submit a form in react native? 如何在React Native中单击提交按钮来验证电子邮件和密码? - How to validate email and password on click of submit button in React native? React Native TextInput手机键盘提交? - React Native TextInput phone keyboard submit? 清除提交时的所有 textInput 字段 react native - clearing all textInput field on submit react native react native-从另一个视图(类)上按下导航栏的“提交”按钮时如何获取表单数据 - react native - how to get form data when pressing on navigation bar submit button from a different view (class) 我知道react-native的TextInput有一个onsubmit回调函数,但我如何实际提交该提交按钮? - I know react-native's TextInput has a onsubmit callback function, but how do i actually make that submit button? 将用户导航到listitem上的New Screen,然后单击React native - Navigate user to New Screen on listitem click React native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM