形式提交,反应钩形式和反应状态

发布于 2025-02-09 15:06:27 字数 1369 浏览 5 评论 0原文

我想拥有一个显示“编辑”以输入编辑模式的按钮,并(使用相同的按钮)“保存”以提交表单。

我的问题是,当按钮“编辑”显示时,请按按钮提交提交

链接到codesandbox 下面的代码。

import "./styles.css";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { Button } from "@material-ui/core";

export default function App() {
  //Setting up state
  const [isEditing, setIsEditing] = useState(false);

  //Handler for entering edit mode.
  const handleEnterEditMode = () => {
    setIsEditing(true);
    console.log("Edit mode entered.");
  };

  //Handler that runs on form submission.
  const handleSave = () => {
    console.log("Form submitted.");
  };

  //Setting up some react-hook-forms variables according to their docs
  const {
    register,
    formState: { errors },
    handleSubmit,
    control,
    reset
  } = useForm();

  //In render:

  return (
    <div className="App">
      <form noValidate onSubmit={handleSubmit(handleSave)}>
        <Button
          {...(isEditing ? { type: "submit" } : {})}
          {...(!isEditing ? { onClick: handleEnterEditMode } : {})}
        >
          {!isEditing ? "Edit" : "Save"}
        </Button>
      </form>
    </div>
  );
}

I would like to have a button that displays "Edit" to enter Edit mode and (using the same button) "Save" to submit the form.

My problem is that it already submits the form upon pressing the button while the button text "Edit" is showing.

Link to codesandbox, containing the code below.

import "./styles.css";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { Button } from "@material-ui/core";

export default function App() {
  //Setting up state
  const [isEditing, setIsEditing] = useState(false);

  //Handler for entering edit mode.
  const handleEnterEditMode = () => {
    setIsEditing(true);
    console.log("Edit mode entered.");
  };

  //Handler that runs on form submission.
  const handleSave = () => {
    console.log("Form submitted.");
  };

  //Setting up some react-hook-forms variables according to their docs
  const {
    register,
    formState: { errors },
    handleSubmit,
    control,
    reset
  } = useForm();

  //In render:

  return (
    <div className="App">
      <form noValidate onSubmit={handleSubmit(handleSave)}>
        <Button
          {...(isEditing ? { type: "submit" } : {})}
          {...(!isEditing ? { onClick: handleEnterEditMode } : {})}
        >
          {!isEditing ? "Edit" : "Save"}
        </Button>
      </form>
    </div>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衬遮猫 2025-02-16 15:06:27

默认情况下,表单元素内部的按钮将始终触发提交事件。您可以将按钮给按钮一个“按钮”的类型属性,或在OnClick方法中添加DestrestDefault()以覆盖此属性。

A button inside of a Form element will always fire a Submit event by default. You can give give the button a type attribute of "button", or add a preventDefault() to the onClick method to override this.

天涯沦落人 2025-02-16 15:06:27

尝试这种方法:

< Button
type = { isSubmiting ? "submit" : null }
>
{!isEditing ? "Edit" : "Save"
} 
</Button>

and also move your onClick condition inside handleEnterEditMode method

Try this approach:

< Button
type = { isSubmiting ? "submit" : null }
>
{!isEditing ? "Edit" : "Save"
} 
</Button>

and also move your onClick condition inside handleEnterEditMode method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文