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.

Discussion: On field change callbacks?

See original GitHub issue

Bug, 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:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jaredpalmercommented, Mar 28, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stimulus 2.0 Value change callbacks. What are they good ...
These are callback methods in the controllers that are called whenever the DOM data- attribute for the corresponding Value changes. I had no ......
Read more >
How to add callback function for lookup Field change?
I want rerender my page when one lookup field changed? I add a js function for the event onchange of the lookup field....
Read more >
reactjs - Are callbacks that are passed into other functional ...
When you are creating your form component, your handleChange function is being updated on every state change, and being passed on to the...
Read more >
Callback when form is updated - Custom IC SKILL
Each field creation function has a ?callback argument which specifies a callback to be called when that field changes (note that it's when ......
Read more >
Change Callback Not Firing as Expected
I'm new to the Stimulus library so apologies for the basic question. I have a controller with a version value that is mutated...
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