Discussion: On field change callbacks?
See original GitHub issueBug, Feature, or Question?
Currently Formik is missing a feature to have a callback when Formik changes a field value. Example when setFieldValue('bip', 'something')
it would be nice to have a callback that would also do the other work necessary.
Current Behavior
Currently I add onChange handlers to my components which uses Formik’s setFieldValue along with my own change handler (imagine if when you select an address, you want to immediately load the shipping cost before submitting the order). However doing that creates a significantly more complex codebase, and I can’t take unit testing shortcuts like calling the setFieldValue prop and checking if all handlers invoked. Additionally when writing components I can make them just generically handle how Formik works and wire all those weird change handlers using the enhanceWithFormik HoC or the Formik component.
Desired Behavior
Just like we have an API for the handleSubmit onSubmit
I am looking for the following APIs:
onChange(values)
onFieldChange({ bip: (value) => {}, bop: (value) => {}})
Thoughts?
Let me know if this jives well with the Formik philosophy. And what you prefer to do instead. If it jives I’ll take a stab at a PR.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:11
- Comments:13 (5 by maintainers)
Hey thanks for taking the time to fill out the feature suggestion. Formik is an uncontrolled component though. Adding onChange would inevitably encourages ppl to try to sync state in 2 places. Please see the form middleware / state reducer RFC.
This is the third or fourth duplicate of this issue/suggestion, so I think it’s time to add a section to the docs.
https://github.com/jaredpalmer/formik-effect