Search code examples
reactjstypescript

What type should I use for a React form onsubmit event?


I have a React application with a form and I'm trying to figure out what type to use for the event handler function for when a form is submitted. Does anyone know what type it is? I tried reading through the React docs, but I couldn't find an example of the type for form submissions, the only examples I saw were for form element's changing their inputs.

function handleSubmit(e // what type should go here?) {
  e.preventDefault();
}

return (
   <form 
      onSubmit={handleSubmit}
   >
      <input
         type='text'
         name='name'
      />
   </form>
)

Solution

  • You can use React.FormEvent<HTMLFormElement>

     function handleSubmit(e : React.FormEvent<HTMLFormElement>){
         e.preventDefault();
     }