title: formik axios post form submission on reactjs
body: <p>Newbie in reactjs ,trying to build a simple form with formik and validating with Yup library .just got a problem ,how to submit axios post request in formik with validations shown in below code.i have confusion on handle submit function onSubmit</p>
  <p><strong>CODE</strong></p>
  <pre><code>import React,{useState} from 'react'
  import axios from 'axios'
  import { toast } from &quot;react-toastify&quot;;
  import { useHistory } from &quot;react-router-dom&quot;;
  import config from '../../utils/config';
  import { useFormik } from 'formik';
  import * as Yup from 'yup';
       export default function AddCompanyPage = () =&gt; {
          let history = useHistory();
            const formik = useFormik({
            initialValues: {name:&quot;&quot;}, 
             validationSchema : Yup.object().shape({
              name: Yup.string()
              .min(2, &quot;*Names must have at least 2 characters&quot;)
              .max(20, &quot;*Names can't be longer than 100 characters&quot;)
              .required(&quot;*Name is required&quot;),
             }),
             onSubmit:values=&gt;
            {
              const AddCompany=async e()=&gt; {
                e.preventDefault();
                axios.post(`${config.APP_CONFIG}/Companies/Company`,values)
                .then(res =&gt; {
                 if(res.data.status_code === 200){
                  //setUser(res.data.msg)
                  history.push(&quot;/settings/companies&quot;);
                 }
                 
               })
               .catch(err =&gt; {
                toast.error(&quot;Couldnot lpost data&quot;)
              }) 
               }
          },
        })
          return (
                &lt;h2 className=&quot;text-center mb-4&quot;&gt;Add a Company&lt;/h2&gt;  
                &lt;form onSubmit={formik.handleSubmit}&gt; 
                  &lt;div className=&quot;form-group&quot;&gt;
                  &lt;label htmlFor=&quot;Company&quot;&gt;Company Name&lt;/label&gt;
                    &lt;input
                      id=&quot;name&quot;
                      name=&quot;name&quot;
                      type=&quot;text&quot;
                      onChange={formik.handleChange}
                      onBlur={formik.handleBlur}
                      value={formik.values.name}
                    /&gt;
                      
             {formik.touched.name &amp;&amp; formik.errors.name ? (
               &lt;div&gt;{formik.errors.name}&lt;/div&gt;
             ) : null}
                  &lt;/div&gt;
                  &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
              &lt;/form&gt;
          )
      }
      
  </code></pre>