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.

onChange for entire form

See original GitHub issue

I have a form where some field’s values depend on other fields values. I have been using redux-form and have been able to calculate these values using an onChange handler on the top-level form object.

I don’t see an equivalent using formik, is this right? I thought I could hijack the validation function to do this, but it’s not passed the formik bag, only the values to validate.

Another usecase for a form-level onChange is automatic persistence without needing to hit a submit button e.g. something like:

<Formik
  initialValues={JSON.parse(localStorage.getItem('formvalues'))}
  onChange={(values) => {
    localStorage.setItem('formvalues', JSON.stringify(values))
  }}
>
{/* … */}
</Formik>

Another option could be a "submitOnChange` flag.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:32 (3 by maintainers)

github_iconTop GitHub Comments

42reactions
benvancommented, Feb 2, 2018

Sorry to reopen a closed thread, but this seemed like the most appropriate place to follow up on this.

If I understand correctly, the suggestion is to use another component to simulate onChange responses. I can see how that might make sense for something extremely agnostic like FormikPersist, but for the general situation where you wish to cause some effect based on form changes, this doesn’t make sense to me.

Consider a simple form which contains dropdowns for the ingredients of a burger. You wish to render a preview of the burger outside the form somewhere. It would seem fairly simple (and conventional) to do this by passing the form an “onBurgerIngredientsChanged” handler, and hooking that up into the form’s onChange handler.

Am I correct in understand you would suggest instead creating a <BurgerIngredientsChangedDetector /> component, complete with constructor, componentDidMount, componentWillReceiveProps methods …

It seems to me this would very naturally lead people to creating their own <OnFormUpdate hander={someHandler}/> component. In this case… why the extra complexity? Would it not be better to be able to pass Formik an onChange handler?

Just my two cents - I’m facing this problem now with a few forms that are slightly more dynamic than their siblings. Any thoughts would be much appreciated.

Thanks

Edit: Furthermore - and I think this is the bigger issue - by binding “onChange” type handlers into the render lifecycle, you’re coupling two very different concepts. My form might render itself every second if it’s being passed a readonly “counter” value - this doesn’t mean any of the values have changed!

42reactions
jaredpalmercommented, Nov 27, 2017

@prichodko is exactly correct.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entire form onChange - javascript - Stack Overflow
A form control that is not a descendant of its form element (possible with the form attribute) will not bubble the event up...
Read more >
onchange Event - W3Schools
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the...
Read more >
HTML Forms Tutorial - HTML onChange - Linuxtopia
onChange specifies script code to run when the data in the input field changes. onChange applies to input fields which accept text, namely...
Read more >
JavaScript : Entire form onChange - YouTube
JavaScript : Entire form onChange [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript : Entire form onChange Note: The ...
Read more >
entire form onchange - WebDeveloper.com Forums
I'm using a large form, and looking way to determine if any of the input objects have changed without supplying an onchange attribute...
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