How to access form component state inside handleSubmit.
See original GitHub issueI 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:
- Created 6 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 usesetStatus
https://github.com/jaredpalmer/formik#setstatus-status-any--voidYour handleSubmit will need to be updated to look something like this:
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-anyHopefully that points you in the right direction. Good luck!
@tplee923 , I share you what I did:
That’s the function passed to
withFormk
and in the component I have this:I use this to make a normal form submit after validating with formik