question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access form component state inside handleSubmit.

See original GitHub issue

I am trying to use setState to add a success message after handleSubmit completed successufully in form component. But it doesn’t works. Please help me to fix it or show me an alternative how to show success messages.

export default withFormik({
  mapPropsToValues ({ email }) {
    return {
      email: email || ''
    };
  },
  validationSchema: Yup.object().shape({
    email: Yup.string().email('Email not valid').required('Email is required')
  }),
  handleSubmit(values, { resetForm, setErrors, setSubmitting }) {
    
    Accounts.forgotPassword({
      email: values.email
    }, (error) => {
      if (error) {
        setErrors({ email: 'Error: ' + error.reason });
      } else {
        this.setState({success: 'Success: Check your inbox for a reset link!'});
        resetForm();
      }
      setSubmitting(false);
    });
  }
})(RecoverPassword);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

38reactions
chrisheningercommented, Dec 19, 2017

Hi Badis,

Since you’re inside the withFormik higher-order component, you’re not going to be able to setState on your RecoverPassword component. Luckily, Formik provides a method for passing results from handleSubmit into your form–  you’ll want to use setStatus https://github.com/jaredpalmer/formik#setstatus-status-any--void

Your handleSubmit will need to be updated to look something like this:

handleSubmit(values, { resetForm, setErrors, setStatus, setSubmitting }) {
    
    Accounts.forgotPassword({
      email: values.email
    }, (error) => {
      if (error) {
        setErrors({ email: 'Error: ' + error.reason });
      } else {
        setStatus({ success: true });
        resetForm();
      }
      setSubmitting(false);
    });
  }

You should then be able to access that value inside your RecoverPassword component by using this.props.form.status https://github.com/jaredpalmer/formik#status-any

Hopefully that points you in the right direction. Good luck!

5reactions
JavoBytecommented, Aug 7, 2018

@tplee923 , I share you what I did:

handleSubmit: (values, { setStatus, setSubmitting }) => {
    setSubmitting(true);
    setStatus({
      success: true,
    });
  }

That’s the function passed to withFormk and in the component I have this:

componentDidUpdate(prevProps) {
    const { success: wasSuccess = false } = prevProps.status || {};
    const { success: isSuccess = false } = this.props.status || {};
    if (isSuccess && !wasSuccess && this.htmlForm) {
      this.htmlForm.submit();
    }
  }

I use this to make a normal form submit after validating with formik

Read more comments on GitHub >

github_iconTop Results From Across the Web

redux-form: Accessing state and form-values on handleSubmit
There is a MyForm component. It has a redux-form inside. Now there is a handleSubmit() function that is defined inside the react component....
Read more >
Forms - React
In React, mutable state is typically kept in the state property of components, and only updated with setState() . We can combine the...
Read more >
useForm - handleSubmit - React Hook Form
This function will receive the form data if form validation is successful. Props. Name, Type, Description. SubmitHandler, (data: Object, e?: Event) => void ......
Read more >
How to get form data on submit in ReactJS - Linguine Code
The form was to save the data in React state when the input value changes. ... I moved my handleSubmit() function inside FooBarForm...
Read more >
React interactivity: Events and state - Learn web development
Handling form submission via callbacks. Inside the top of our App() component function, create a function named addTask() which has a single ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found