Category: Next.js, React and TypeScript

React: Clicking the second button accidentally sends the form

Read this guide in Polish

The button opening the preview in the middle of the form calls a submit and saves incomplete data. The button element without a specified type has the default semantics submit inside the form.

How to identify the source of the problem

Check the DOM structure, not just the JSX component, and determine which button triggers the submit event. Track event subject and number of action calls or handlers in DevTools.

Compare the message from the console with the result of the production compilation and the condition after the page has been refreshed.

Repair steps

Set the auxiliary button as type="button"; only the writing button leave type= "submit". Add validation on the server side and block duplicate entries if you click multiple times.

How to Test Solution

Clicking the preview does not send the request, and the record only takes place on a conscious submit.

Security and restrictions

Do not remove Enter support from the form instead of setting the correct button type.