简体   繁体   中英

Submitting form returns proxy object instead of form data in Reactjs

I am creating a login form using semantic-ui in Reactjs. Please find the code below:

The the login form itself:

import React from 'react';
import { Form, Button } from 'semantic-ui-react';

const LoginPage = ({ email, password, handleChange, handleSubmit, errors }) => (
    <Form onSubmit={handleSubmit}>
        <Form.Field>
            <label htmlFor="email">Email:</label>
            <input
                type="email"
                name="email"
                id="email"
                placeholder="example@example.com"
                value={email}
                onChange={(e) => handleChange(e)}
            />
        </Form.Field>
        <Form.Field>
            <label htmlFor="password">Password:</label>
            <input
                type="password"
                name="password"
                id="password"
                value={password}
                onChange={(e) => handleChange(e)}
            />
        </Form.Field>
        <Button primary> Login </Button>
    </Form>
);

export default LoginPage;

The login container (parent component) is as below:

import React, { Component } from 'react';
import { LoginPage } from '../components';
import Validator from 'validator';

class Login extends Component {

    constructor(props) {
        super(props)
        this.state = {
            data: {
                email: '',
                password: ''
            },
            loading: false,
            errors: {}
        }
    }

    handleChange = (e) => {
        this.setState({
            data: { ...this.state.data, [e.target.name]: e.target.value }
        });
    }

    handleSubmit = (values) => {
        console.log(values);
        const errors = this.validate(this.state.data);
        this.setState({
            errors: errors
        })
    }

    validate = (data) => {
        const errors = {};
        if (!Validator.isEmail(data.email)) errors.email = "Invalid Email";
        if (!data.password) errors.password = "Password cannot be blank";
        return errors;
    }

    render() {
        return (
            <LoginPage
                email={this.state.data.email}
                password={this.state.data.password}
                handleChange={this.handleChange}
                handleSubmit={this.handleSubmit}
                errors={this.state.errors}
            />
        )
    }
}

export default Login;

When I try to console log values in the handleSubmit function of the parent component it always returns proxy object instead of form values or form data.

Proxy {dispatchConfig: {…}, _targetInst: ReactDOMComponent, isDefaultPrevented: ƒ, isPropagationStopped: ƒ, _dispatchListeners: ƒ, …}

Could anyone let me know where am I going wrong ?

Thanks

The "Proxy" object seems to be an Event object. Indeed the semantic-ui docs say

Our handles data just like a vanilla React . See React's controlled components docs for more.

and here is the vanilla react example it refers to:

handleSubmit(event) {
  alert('A name was submitted: ' + this.state.value);
  event.preventDefault();
}

So with this library it is your own duty to retrieve the form data from your state/store, it is not passed to onSubmit automatically.

handleSubmit = (e) => {
    console.log(e);
    const errors = this.validate(this.state.data);
    this.setState({
        errors: errors
    })
}

So actually it is correct as you have it, because you are not using this parameter e / values anywhere. You were only confused by the log and your variable name. You could simply omit it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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