简体   繁体   English

此表格如何提交?

[英]How does this Form gets submited?

Im currently trying to figure out, how this Form actually gets submitted without having an Input type of submit or an event handler that triggers the submission.我目前正试图弄清楚,这个表单是如何在没有提交的输入类型或触发提交的事件处理程序的情况下实际提交的。

For my understanding this form does not get submited anyhow.据我了解,无论如何都不会提交此表格。 Its just getting re-rendert with new data.它只是用新数据重新渲染。 Somehow this looks tricky to me.不知何故,这对我来说看起来很棘手。

Could someone give me explanation where this gets evaluated?有人可以给我解释这是在哪里评估的吗?

Index.js索引.js

import React, { useState } from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";
import Header from "./Header";
import Mui from "./Mui";
import ButtonsResult from "./ButtonsResult";
import { EditorState } from "draft-js";
import "react-datepicker/dist/react-datepicker.css";
import "antd/dist/antd.css";
import "./styles.css";

let renderCount = 0;

const defaultValues = {
  Native: "",
  TextField: "",
  Select: "",
  ReactSelect: { value: "vanilla", label: "Vanilla" },
  Checkbox: false,
  switch: false,
  RadioGroup: "",
  DraftJS: EditorState.createEmpty(),
  MUIPicker: new Date("2020-08-01T00:00:00")
  MUI_Slider: [0, 10]
};

function App() {
  const { handleSubmit, reset, setValue, control } = useForm({ defaultValues });
  const [data, setData] = useState(null);
  renderCount++;

  return (
    <form onSubmit={handleSubmit((data) => setData(data))} className="form">
      <Header renderCount={renderCount} />

      <Mui control={control} />

      <div className="container">
        <ButtonsResult {...{ data, reset, setValue }} />
      </div>
    </form>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

ButtonResult.js ButtonResult.js

import React from "react";
import { convertToRaw } from "draft-js";

export default ({ data, reset }) => {
  return (
    <>
      {data && (
        <pre style={{ textAlign: "left", color: "white" }}>
          {JSON.stringify(
            {
              ...data,
              DraftJS: convertToRaw(data.DraftJS.getCurrentContent()).blocks[0]
                .text
            },
            null,
            2
          )}
        </pre>
      )}

      <button
        className="button buttonBlack"
        type="button"
        onClick={() => {
          reset({
            Native: "",
            TextField: "",
            Select: "",
            ReactSelect: { value: "vanilla", label: "Vanilla" },
            Checkbox: false,
            switch: false,
            RadioGroup: "",
            MUIPicker: new Date("2020-08-01T00:00:00"),
            MUI_Slider: [0, 10]
          });
        }}
      >
        Reset Form
      </button>
      <button className="button">submit</button>
    </>
  );
};

Live Example现场示例

编辑 React Hook 表单 - V7 - 控制器(分叉)

The default type value for button elements is "submit." button元素的默认type值为“提交”。 Since the second button does not have a type attribute assignment in the markup, it is a "submit" button.由于第二个按钮在标记中没有type属性赋值,所以它是一个“提交”按钮。

As per https://www.w3.org/TR/html401/interact/forms.html#h-17.5 (found via What is the default button type? ) the default type of a button is "submit".根据https://www.w3.org/TR/html401/interact/forms.html#h-17.5 (通过What is the default button type?找到),按钮的默认类型是“提交”。 Also, the form has an "onSubmit" handler, hence clicking the "submit" button will invoke that handler.此外,表单有一个“onSubmit”处理程序,因此单击“提交”按钮将调用该处理程序。 In this case, the handler only sets the state:在这种情况下,处理程序只设置 state:

(data) => setData(data)

And that explains the re-rendering of the form.这就解释了表单的重新渲染。

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

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