简体   繁体   English

React 钩子表单在取消时提交

[英]React hook form submit on cancel

I am using React Hook form.我正在使用 React Hook 表单。 I have a button cancel with a OnCancel function.我有一个带有 OnCancel 功能的按钮取消。 Basically React Hook Form auto submit on cancel.基本上 React Hook Form 在取消时自动提交。 Why ?为什么 ? is there a way to block the submit on cancel ?有没有办法在取消时阻止提交?

    const onCancel = () => {
    history.goBack();
};


<StyledButton onClick={onCancel}>
    {Translate('cancel')}
</StyledButton>
import "./styles.css";

export default function App() {
  const dontSubmit = (e) => {
    // e.preventDefault();
    console.log('Dont submit this');
  }


  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>


      <form>
        <input type="text" />
        <button type="button" onClick={dontSubmit}>Do not submit!</button>
      </form>
    </div>
  );
}

You can either give your button the property type="button" see example code.你可以给你的按钮属性type="button"查看示例代码。 Or you can add e.preventDefault() to your function.或者您可以将e.preventDefault()添加到您的函数中。 See the commented code.请参阅注释代码。

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

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