-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make onSubmit/submitForm async #1229
Conversation
Hey, any updates on how this is going? |
Noob question: how can I pull this branch? |
@devjones Appreciate the help but I have an issue using your forked repo. The file/folder structure between your repo that I run Namely the I get this error after running
I feel I may be missing a step. |
@Nemsae You also need to build the package w/ |
Fix conflicts por favor |
@jaredpalmer Conflicts resolved. |
@@ -445,16 +445,17 @@ export class Formik<Values = FormikValues> extends React.Component< | |||
this.setState({ isValidating: false }); | |||
const isValid = Object.keys(combinedErrors).length === 0; | |||
if (isValid) { | |||
this.executeSubmit(); | |||
return Promise.resolve(this.executeSubmit()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wrap this with Promise.resolve
? Isn't the result the same without it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guarantees that a promise is returned. If this.props.onSubmit
were to return void
instead of a promise, this would ensure chaining.
@@ -445,16 +445,17 @@ export class Formik<Values = FormikValues> extends React.Component< | |||
this.setState({ isValidating: false }); | |||
const isValid = Object.keys(combinedErrors).length === 0; | |||
if (isValid) { | |||
this.executeSubmit(); | |||
return Promise.resolve(this.executeSubmit()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This guarantees that a promise is returned. If this.props.onSubmit
were to return void
instead of a promise, this would ensure chaining.
} else if (this.didMount) { | ||
// ^^^ Make sure Formik is still mounted before calling setState | ||
this.setState({ isSubmitting: false }); | ||
} | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this intentional to return void
when there are validation errors? This would imply that consumer is checking the result of submitForm()
before chaining to it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree we should rather harmonize on a single return type like Promise<Result | null>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Promise<Result | undefined>
Looks like the types for https://github.com/devjones/formik/blob/master/src/types.tsx#L105 |
@jaredpalmer do you plan to support this long awaited feature on 1.x? That would be helpful as people can't necessarily migrate to hooks immediately and it's not yet a stable release (I'm doing Expo/RN in my case). I would be fine with a workaround until 2.x if you have one to share, but unfortunately I don't really see how to easily reproduce the behavior inside |
@slorber planning to release v2 at React Europe |
Great! As hooks are going in Expo 33 that should sync well |
@jaredpalmer when is React Europe? Is it the one in May 2020? Because that's a crazy long wait for something like this. |
@ffMathy as you can see there are RC's being published and actively worked on: https://github.com/jaredpalmer/formik/releases |
@slorber ah very cool! Is there an ETA on a release date? |
@ffMathy Feel free to review active PRs and contribute. We could use the help! |
This behavior is in v2 right now btw. |
Hi, all!
@jaredpalmer have a question. I've updated to 2.0.1-rc.7 and trying to use this async // SomeContainer.tsx
submitHandler = async (
values,
{ setErrors }: FormikHelpers<IMappedDefaultModelDataShape>,
) => {
// ...some code here
try {
return await update(id, values);
} catch (err) {
throw Error(err);
}
};
render() {
return <Formik {...this.props} onSubmit={this.handleSubmit}></Formik>
} // SomeFieldRenderer.tsx
const onSaveButtonClick = async () => {
const { submitForm } = props.formikActions;
return submitForm()
.then(promise => {
// i'm always getting here
})
.catch((e: any) => {
//cant get here
});
}; As I see from source code inside const submitForm = useEventCallback(() => {
// ...some code here
return validateFormWithHighPriority().then(
// ...some code here
return Promise.resolve(executeSubmit())
.then(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_SUCCESS' });
}
})
.catch(_errors => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
}
// I suppose we have to throw here in order to catch properly in my `onSaveButtonClick` method
});
// ...some code here
return;
}
);
}, [executeSubmit, validateFormWithHighPriority]); Why do we not throwing error from |
@andrewMuntyan Not sure. Let's make a new issue with a repro. |
here #1636 I've created a new one |
This is now in v2. |
This PR enables chained promises with the submitForm imperative method. It will wait until any optional promises returned by a form's handleSubmit is resolved, before executing a chained promise. Such as:
The async implementation in v2.0 canary does not support expected behavior of chainable promises since the promise in that function is never returned. Therefore in that implementation,
doSomethingAfterSubmit()
does not wait for resolution of ahandleSubmit
promise.See below for the impact of return statements in Promise handler functions:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Return_value