首页 > 解决方案 > 如何在有条件的本地反应中使用导航

问题描述

我是本地人的新手。我很困惑如何在本机反应中实现导航,但条件是在检查表单验证成功后导航将起作用。这里我使用 react-native: 0.61.5

import React, {Component} from 'react';
import {Text, View} from 'react-native';

export default class Apps extends Component{

    state = {
        username: '',
        emailAddress: '',
        password: '',
        msgValidasi: '',
    };

    checkValidasi = () => {
        let email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;

        if(this.state.username.trim() === '' && this.state.emailAddress.trim() === '' && this.state.password.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in the Form Below!' })

        }else if(this.state.username.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Username!' })

        }else if(this.state.emailAddress.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Email Address!' })

        }else if(email.test(this.state.emailAddress) === false) {
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Incorrect Email Format!' })

        }else if(this.state.password.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Password!' })

        }else if(this.state.password.length < 6){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Password At least 6 characters!' })
        }else{
            this.setState({ msgValidasi: 'success' })
        }

    render(){
        return(   
             <View style={{alignItems: 'center'}} >
                  <LinearGradient
                      colors={['#8b9cea','#657eee','#637cee', '#485aee']}
                      start={{x: 0.0, y: 1.0}} end={{x: 1.0, y: 2.0}}
                      style={{width: 150, borderRadius: 20}} >

                          <TouchableWithoutFeedback onPress={this.checkValidasi}>
                               <Text style={{
                                   textAlign: 'center',
                                   padding: 10,
                                   color: 'white',
                                   fontWeight: 'bold'}} >Sign Up</Text>
                          </TouchableWithoutFeedback>

                   </LinearGradient>
              </View>
        )
    }
}

当按下 onPress 键时,它将运行检查checkValidasi()方法。如果成功,则页面将移至 for example.js。我应该在哪里实现这个代码 () => this.props.navigation.navigate ('Home')??

到目前为止,我只能进行验证检查,但检查成功时无法移动页面

标签: react-nativereact-native-android

解决方案


尝试这个

import React, {Component} from 'react';
import {Text, View} from 'react-native';

export default class Apps extends Component{

    state = {
        username: '',
        emailAddress: '',
        password: '',
        msgValidasi: '',
    };

    checkValidasi = () => {
        let email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;

        if(this.state.username.trim() === '' && this.state.emailAddress.trim() === '' && this.state.password.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in the Form Below!' })

        }else if(this.state.username.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Username!' })

        }else if(this.state.emailAddress.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Email Address!' })

        }else if(email.test(this.state.emailAddress) === false) {
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Incorrect Email Format!' })

        }else if(this.state.password.trim() === ''){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Please Fill in Your Password!' })

        }else if(this.state.password.length < 6){
            this.refs.alertNull.open()
            this.setState({ msgValidasi: 'Password At least 6 characters!' })
        }else{
            // this.setState({ msgValidasi: 'success' })
            this.props.navigation.navigate ('Home')
        }

    render(){
        return(   
             <View style={{alignItems: 'center'}} >
                  <LinearGradient
                      colors={['#8b9cea','#657eee','#637cee', '#485aee']}
                      start={{x: 0.0, y: 1.0}} end={{x: 1.0, y: 2.0}}
                      style={{width: 150, borderRadius: 20}} >

                          <TouchableWithoutFeedback onPress={this.checkValidasi}>
                               <Text style={{
                                   textAlign: 'center',
                                   padding: 10,
                                   color: 'white',
                                   fontWeight: 'bold'}} >Sign Up</Text>
                          </TouchableWithoutFeedback>

                   </LinearGradient>
              </View>
        )
    }
}

推荐阅读