提交时反应放置

问题描述 投票:0回答:2

我看过多个在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”属性,但这也不起作用。

reactjs forms onsubmit
2个回答
1
投票

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


1
投票

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


0
投票

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

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

ReactDOM.render(
  <form onSubmit={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</button>
  </form>,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>
© www.soinside.com 2019 - 2024. All rights reserved.