简体   繁体   中英

React hook form submit on cancel

I am using React Hook form. I have a button cancel with a OnCancel function. Basically React Hook Form auto submit on cancel. 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. Or you can add e.preventDefault() to your function. See the commented code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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