-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Bug: manually created 'change' events via new Event() don't trigger React event handlers #19846
Comments
React |
Sure yes First just to clarify: We do want React's notion of onChange -- events for every character typed. My expectation is just that a manually created I think it's worth being thorough here so my answer doesn't just raise more questions. We have around 40 custom form input components -- many contain native inputs, some don't. We used to use In our setup we use a custom Note: I just did a test and I'm seeing both events firing for text inputs and for autocomplete events, as well as a few others. Our assumptions may be wrong or out of date... Maybe we could move towards standardizing to use Custom values on eventsIn the example of our CheckboxGroup component, the we want the
So I guess my questions are
My codepen above was the minimal repro. What I'm actually working on is making a nice encapsulated way of dealing with all the details required to emit events with complex values. I've got a draft component that uses
It works for |
This is incorrect, since React
I haven't read in depth but my initial take is that you need to reconsider the approach and move away from emitting custom events. This is whack-a-mole where you're trying to guess implementation details that keep changing. Can we refocus on the initial product problem? E.g. the autocomplete issues — can we get a minimal repro of that? And if they're not relevant, what's stopping you from simply always using React |
OK, that does make sense. We can mostly do what we want with Fine to close this ticket then. Thanks! I'll still answer this though:
Custom events have been useful to us for two reasons:
As a bit of context on (1): I'm at Redfin and we may have an unusually strong interest in encapsulating complexity in components vs. just writing more store code. |
FWIW others struggle with this as well and React issue #11488 Is there any way to manually create a React |
I understand the desire for encapsulated components, the part I don't understand is why you want to fire native browser events instead of exposing your own component's events via props. |
We do use
If we were starting from scratch we could use Context to accomplish a lot of this, but I'm not convinced it's a better path. In our case, switching implementations would be expensive and risky, so just trying to continue to make it cleaner and easier to use without a drastic change. |
Here's an illustration of that scenario of logging Say you're logging Using event bubbling you can make a global setup where you handle sections like
and deep down in there, some component could do
then TrackingSection could have it’s own handlers where it does
and then we log the event when it hits the top of the page if the event has all the expected parts. If the section needs to discern between events, only log some and not others, or attach extra info for some, that can all happen right there and everything further down the tree can be agnostic about where they're used - don't have to thread extra props through or read from Context or import some section-specific tracking util. It's a really lightweight way of letting a button or input component say, "this happened, if you care" and letting a section say "I do care, thanks". And if you need to modify the tracking at more places in the tree that's easy too. |
I would also like to be able to do this. I am building an AngularJS-in-React bridge, and I'm mapping Angular's concept of two-way binding to React change events. This allows React to see each bridged Angular component as something akin to a I recognize that the very idea of this is unpleasant, but it is helping my team refactor our legacy AngularJS components into React from the top down. I'm also using an |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
This is also useful for using native web components which can fire a
It is surprising that React is not listening to This unfortunate implementation detail of react enforces developers to use a |
@gaearon Are there any instructions on how to properly create synthetic event based on change event of input element? |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
bump |
So, 4 years have passed. Is there any canonical way to trigger react's onChange handler? |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
Description:
Manually created events created via
new Event
and emitted from a hidden input work great for'input'
events, they bubble as expected and can be caught viaonInput
handlers, but using'change'
events this way doesn't work --onChange
handlers are never called.The vanilla JS
'change'
events do bubble normally and can be caught by parents with vanilla JS listeners ( usingaddEventListener
), but the ReactonChange
listeners don't register anything.I've created a codepen to demonstrate a minimal case for this via console logging. See repro steps below and code comments for additional details.
React version: 16.13.1
Steps To Reproduce
change
event being emitted from a child component after render. We expect a second log from a parent'sonChange
but it never comes.EVENT_TYPE
to'input'
onInput
handler in the parent component.EVENT_TYPE
back to'change'
and uncomment that code and you'll see that that listener does workThe text was updated successfully, but these errors were encountered: