-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Trigger change events when the value of an input is changed programmatically #1152
Comments
Correct, this mirrors how events are fired outside of React and is expected. Having value changes trigger a |
what about manually call setState after change the input value by maybe I want this
|
So, fwiw, my first attempt at solving this issue given a 3rd party component that I don't want to neither modify nor reach too deeply in to, has been to use http://facebook.github.io/react/docs/test-utils.html#simulate. Not sure how much this is adding to my Webpack bundle. |
I have a generic form action that saves the values of a form to state when they are changed. However, I have an option to save the details in localStorage so next time a user goes to the form its pre-filled. However, it seems using ReactDOM to pre-fill the values from storage doesn't trigger the onChange Event and my state isn't updated, and thus submitting a form fails with blank values. |
@stevematdavies |
I have redux action which saves user login credentials to localStorage when a check box is checked. If the user logs out, and returns to the log in page. The values themselves are read into the forms state when the user enters them into the fields by using the onChange event of each field. I did it this way as I have many forms basically the same except for the form name. so I use one parent ApplicationForm component form which has a function:
the $ is so I can avoid setting state values for selected fields if necessary. Thus to use this common functionality, I just drop my child forms in to the ApplicationForm as its children (rendered by this.props.children) Thus any field inside this ApplicationForm will be harvested with onChange, including other items such as checkboxes, reCaptchas etc The issue is, when a value is entered into any field via storage, or programmatically eg getting them from localStorage), the onChange event is not fired (as you mentioned) and thus the values are not added to state, and when submit is fired they are all undefined. Of course I could circumvent this with parsing the values on submit, however, this means I would have to ensure I know the names of all the fields of all the child forms, and ensure they are all unique. This is just silly replication, and not abstract enough for our coding standards. |
@dahlke How did you solve this?? I have raised a similar question in stackoverflow |
When I bind an event to an input using
onChange
and then change the value of the input programmatically, a change event is not fired.Example:
Changing the input with:
Does not result in a triggering of
on_change
. This has to be done manually, like so:The text was updated successfully, but these errors were encountered: