Trigger submit on change #8486
Unanswered
marcobiedermann
asked this question in
Ideas
Replies: 2 comments 5 replies
-
could you do something like this? useEffect(() => {
watch((dta) => {
// you can throttle the request here as well, and without any render to the UI
saveData(data)
})
}, [watch]) |
Beta Was this translation helpful? Give feedback.
5 replies
-
After trying out @k-zakhariy's method, which worked but for my case didn't see the need of having a custom file for that, I just tweaked the code a bit and used it in one of my form like this: import { useDeepCompareEffect } from 'use-deep-compare';
import debounce from 'lodash.debounce';
const debouncedSave = useCallback(
debounce(() => {
form.handleSubmit(props.onSubmit)();
}, 1000),
[form, props.onSubmit],
);
const watchedData = form.watch();
useDeepCompareEffect(() => {
if (form.formState.isDirty) {
debouncedSave();
}
}, [watchedData]); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
It would be great to have an option to call
handleSubmit
on change instead of form submission.This would allow the form to be submitted if any of the fields change e.g. auto-save/auto-search.
There is already an option for validation but not for submitting the form.
Listening on change is currently only possible via
watch
Proposal
References
Formik auto-save: jaredpalmer/formik#1218 (comment)
Beta Was this translation helpful? Give feedback.
All reactions