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

State and Effect Hooks

SpongeBob gif from the Season 1, Episode 20 “Hooky.”

API automation testing using CodeceptJS, TypeScript and Got

Using Closure Compiler’s ADVANCED_OPTIMIZATIONS With Webpack

How to use Ansible Jinja template

Why You Should Consider Angular Development For Your design

Transform MongoDB collections automagically into Graphs

The More Complete Guide to Master React  —  Part 1

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

Dear Web Devs, Please Use Anchors Instead of Divs for Buttons

Demystifying Web Workers

Working of web worker

An investigative guide to React JS[DOM, Virtual DOM and JSX] Part-IV

How To Write An Integration Test With Jest/RTL