How to use an SVG to submit a form with Formik + Typescript

Rasmus Rimestad
Feb 18, 2021

We had a case where we wanted an SVG (wrapped in the component YesIcon) to act as a submit button in Formik. We had trouble getting the onClick={handleSubmit}to work on the SVG itself as Typescript said that the event types did not match

After some trial and error, this is what we ended up doing:

<Formik
initialValues={initialValues}
onSubmit={(values) => console.log(values)}
>
{({handleSubmit}) => (
<Form>
{/* Other inputs here */}
<Field as="span" onClick={handleSubmit}>
<YesIcon/>
</Field>
</Form>
)}
</Formik>

--

--