简体   繁体   English

ReactJS表单中的handleSubmit中的问题

[英]Problems in handleSubmit in Form in ReactJS

I am creating a login system by ReactJS. 我正在用ReactJS创建一个登录系统。 I have a problem while creating the login page. 创建登录页面时出现问题。 The following is my code: 以下是我的代码:

export default class Login extends Component {
    state={
        data:{
            email:'',
            pass:''
        },
        loading:false,
        error:{}
    }
    handleChange=(e,{name,value})=>{
        this.setState({data:{[name]:value}});
        // console.log(this.state.data);
    }
    handleSubmit=()=>{
        const error=this.validate(this.state.data);
        this.setState({error:error});
        (_.isEmpty(this.state.error))
        ?(
        console.log(this.state.data)
        )
        :(console.log('Cannot submit'));
    }
    validate=(data)=>{
        const err={};
        if (!validator.isEmail(data.email)) {
            err.email='Invalid email'
        }
        if (!data.pass) {
            err.pass='Your password is missing'
        }
        return err;
    }
    render() {
        const {error}=this.state;
        return (
            <div className='login-form'>
                <Grid textAlign='center' style={{height:'100%', marginTop:80}} verticalAlign='middle'>
                <Grid.Column style={{maxWidth:500}}>
                <Form>
                <Segment raised textAlign='left'>
                <Header as='h2' color='blue' textAlign='center'>Please fill in login form</Header>
                <Form.Input fluid icon='mail' iconPosition='left' type='email' placeholder='Email' name='email' id='email' value={this.state.data.email} onChange={this.handleChange} required/>
                {(error.email)?(<InlineError text={error.email}/>):('')}

                <Form.Input fluid icon='privacy' iconPosition='left' type='password' placeholder='Password' name='pass' id='pass' value={this.state.data.pass} onChange={this.handleChange} required/>
                {(error.pass)?(<InlineError text={error.pass}/>):('')}
                <br/>
                <Checkbox label='Remember me'/>
                <br/>
                <Button color='blue' fluid size='medium' type='submit' onClick={this.handleSubmit}>Login</Button>
                </Segment>
                </Form>
                <Message attached='bottom' warning floating>New to us? <Link to='/signup'><a>Signup</a></Link></Message>
                </Grid.Column>
                </Grid>
            </div>
        )
    }
}

In my case, I want the form to be submitted successfully by displaying the user data in the console only when the {this.state.error} is empty. 就我而言,我希望仅在{this.state.error}为空时才通过在控制台中显示用户数据来成功提交表单。 However, when I clicked the login button the first time, which I left an error intentionally, the form was submitted successfully and data was displayed in the console. 但是,当我第一次单击登录按钮时,我故意留下了一个错误,该表单已成功提交,并且数据显示在控制台中。 I clicked it again the error was then displayed. 我再次单击它,然后显示错误。 Why was not the error shown in the first place? 为什么没有首先显示该错误? Can you tell me what is wrong with my code? 您能告诉我代码有什么问题吗?

I think you need to do event.preventDefault() in 我认为您需要在以下位置执行event.preventDefault()

handleSubmit=(event)=>{
        event.preventDefault()
        const error=this.validate(this.state.data);
        if(error) {
           this.setState({error:error});
           return;
        }
        (_.isEmpty(this.state.error))
        ?(
        console.log(this.state.data)
        )
        :(console.log('Cannot submit'));
    }

because your button type is submit and it will reload the page 因为您的按钮类型是“提交”,它将重新加载页面

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM