How to store an image on the server using React useFormik Hook

Parshad Doshi
Bitontree
Published in
2 min readNov 19, 2020

I am going to explain in this article how you can implement useFormik hook to store an image on the server. Formik uses useFormik to create the <Formik> component internally. You must only use this hook if you are NOT using <Formik> or withFormik.

To implement the useFormik hook, you can install Formik with NPM or Yarn.

Formik is compatible with React v15+ and works with ReactDOM and React Native. Formik is a small library that helps you with the 3 challenging tasks:

  1. Getting values in and out of form state
  2. Validation and error messages
  3. Handling form submission

We pass our form’s initialValues and a submission function (onSubmit) to the useFormik() hook. The hook then returns a goodie bag of form state and helpers in a variable we are calling formik. In the goodie bag, there are a bunch of helper methods, but for now, the ones we care about are as follows:

  • handleSubmit: A submission handler
  • handleChange: A change handler to pass to each <input>, <select>or <textarea>
  • values: Our form's current values

Now assuming that you have written the backend code which is used to store the image at your desired location on the server, I have used the link for the backend that will store the image on the server in the App component as shown below. It also uses the axios library.

After writing the necessary import, you need to write the following code and it will store the image using useFormik hook on the server.

App Component

In the given code, I have tried to keep the code as simple as possible to help easy implementation. You will need to change the Backend URL for your API endpoint and it will be implemented.

--

--