How to Handle a Submit for a Form

const postSubmit = (e, title, description, image, category) => {   //prevent the page from refreshing 

const token = localStorage.token;
fetch("*backend link here*", { method: "POST", headers: { "Content-Type": "application/json", },

//stringify your inputs into JSON format
body: JSON.stringify({ userId: userID, title: title, description: description, image: image, category: category, }), })
//after the fetch is done, receive a json response object
.then((response) => response.json())

//set the response data to state inside of react
.then((data) => setData(data)) .catch((err) => {console.log("error posting" + err);});};
<buttontype="submit"onClick={(e) => {handleLogin(e, user, password);}}className="border border-solid rounded-full py-1 px-3 transform translate-y-3 text-white transition-shadow  duration-300 ease-in-out uppercase hover:shadow-lg"style={{ backgroundColor: "#ff9f1c", borderColor: "#ff9f1c" }}>Log in</button>
<form onSubmit={(e) => handleSubmit(e, user, password)}>   //code here 

Flatiron student, word nerd and otter enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build a Headless WordPress using 11ty

What is JavaScript And Use case of JavaScript?

What happens when you type a in your browser and press Enter?

Improving my Festivalocator App with jQuery


Events in JavaScript

READ/DOWNLOAD@! JavaScript: The Good Parts FULL BO

JS211 — Blog 202

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Crystal Villanueva

Crystal Villanueva

Flatiron student, word nerd and otter enthusiast

More from Medium

Experiments in hard realtime

On Serialization in Applications

How to get started with vim(neovim) for web development in 2021

How to divide webpack into development and production