By Default, whenever a button in clicked inside a form, a HTTP request is send to server and the page reloads.
To remedy that:
change default button type from submit to button.
OR
can add onSubmit function to form instead of onClick function to button. And add event.preventDefault() to the start of handler Function. This cancels event is possible.
Then we can further send collected datato a standalone API instead of react server.
Simple connect to email and password and console.
But resetting input fields can be hard using refs. It's not recommended.
Also will be increasingly difficult as input fields increase.
FormData simplifies collecting form data, especially when handling multiple checkboxes or inputs.
Converting FormData to an object with Object.fromEntries(fd.entries()) makes it easy to work with the data in your JavaScript code.
function handleSubmit(event) { event.preventDefault(); // get input data with name fields const fd = new FormData(event.target); const data = Object.fromEntries(fd.entries()); // for multiple checkboxes with same name. use .getAll() const acquisitionChannel = fd.getAll("acquisition"); data.acquisition = acquisitionChannel; console.log(data); }
Also add a quick little reset code to reset the form
function handleSubmit(event) { // ... reset inputs event.target.reset();}