对onSubmit放置进行反应

时间:2019-10-30 03:06:44

标签: reactjs forms onsubmit

我已经查看了多个在React中使用onSubmit函数的资源,它们总是将onSubmit触发器放置为Form属性,就像这样

import React, { Component } from "react";

import { connect } from "react-redux";
import PropTypes from "prop-types";
import { register } from "../../../actions/auth/auth.action";

import { Container } from "react-bootstrap";
import {
    Button,
    Form,
    Label,
    Input,
  } from "reactstrap";

class Register extends Component {
    state = {
        fname: "",
        lname: "",
        email: "",
        password: ""
    };

    static propTypes = {
        register: PropTypes.func.isRequired
    };

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

    handleSubmit = e => {
        e.preventDefault();

        const { fname, lname, email, password } = this.state;

        const newUser = {
            name: {
                first: fname,
                last: lname
            },
            email: {
                address: email
            },
            password
        };

        // Attempt to register user
        this.props.register(newUser);
    };

    render() {
        return(
                        <Form onSubmit={this.handleSubmit}>
                                <Label for="fname">First Name</Label>
                                <Input
                                    type="text"
                                    name="fname"
                                    id="fname"
                                    placeholder="Enter first name"
                                    className="mb-3"
                                    onChange={this.handleChange}
                                />

                                <Label for="lname">Last Name</Label>
                                <Input
                                    type="text"
                                    name="lname"
                                    id="lname"
                                    placeholder="Enter last name"
                                    className="mb-3"
                                    onChange={this.handleChange}
                                />

                                <Label for="email">Email</Label>
                                <Input
                                    type="email"
                                    name="email"
                                    id="email"
                                    placeholder="Enter email"
                                    className="mb-3"
                                    onChange={this.handleChange}
                                />

                                <Label for="password">Password</Label>
                                <Input
                                    type="password"
                                    name="password"
                                    id="password"
                                    placeholder="Enter password"
                                    className= "mb-3"
                                    onChange={this.handleChange}
                                />
                                <Button color="primary" style={{ marginTop: "2rem" }} block>
                                    Register
                                </Button>
                        </Form>
        );
    };
};

const mapStateToProps = state => ({
    error: state.error   
});

export default connect(
    mapStateToProps,
    { register }
)
(Register);

但是,当我尝试这种方法时,无法调用该函数。 (即,当调用是表单属性时,按钮如何“知道”何时调用onSubmit函数)我什至尝试向按钮添加type =“ submit”属性,但这也不起作用。

2 个答案:

答案 0 :(得分:1)

只需在按钮{{1}中声明type =“ submit”,onSubmit表单正在等待type属性:),然后从<button type="submit">send stuff</button>方法中删除e.preventDefault,然后重试

答案 1 :(得分:1)

在提交事件发生时将调用该函数。

下面的代码显示触发提交事件的动作。

<top (required)>'
/home/myappuser/.rvm/gems/ruby-2.5.0/bin/ruby_executable_hooks:24:in
class FormComponent extends React.Component {

  handleSubmit = e => {
    e.preventDefault();
    console.log('submitted');
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <input type="submit" /><br />
        <button>Button with no attribute</button><br />
        <button type="submit">submit typed Button</button><br />
        <button type="button">button typed Button (no submit)</button><br />
        <input type="text" value="Press enter to submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <FormComponent />,
  document.getElementById("react")
);