-
Notifications
You must be signed in to change notification settings - Fork 100
Reinstall custom event handlers when component updates #158
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
Conversation
This adds support for changing custom event handlers after the component has mounted. On every update of the component, the event handlers in the previous and current props are compared and (un)installed accordingly. Fixes: RIP21#156
README.md
Outdated
//... Add any codeMirror events | ||
}} | ||
/> | ||
``` | ||
|
||
If changed, the event handlers are reinstalled on component update. Make sure to use instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, but in the example above object will be different every time the component rerenders, so it will definitely cause all event handlers to reinstall on any rerender of the parent, so events need to be wrapped in useMemo
along with useCallback
for event handlers, so only when they are really changed this object will be recreated, otherwise, they'll keep changing.
And, yeah, they'll keep reinstalling themselves on value
change too, so the comparison algorithm should be more sophisticated than what it's now.
If you checked it and it all works, I'll be glad to merge and publish a new version :) For me, it looks like on any prop change it will remove and add event handlers back again for no reason, not only those that have changed. So I'm unsure TBH :) |
I had checked it in the demo and it appeared to work correctly with something like
but not with something like
In the latter case the blur handler would be removed and re-added on every render. That's what the note in the README was supposed to warn about. I realized this wouldn't really be a great change for existing users. Anyone using the component with event handlers like in the second snippet would experience that pointless remove & re-add on every rendering. I pushed a new variant to address this. The component now wraps the event handlers in a persisted function and resolves them at runtime via |
Whatever, I'm going to rewrite it now anyway (I have some time) to be using hooks, so I'll try to test it nicely and use this code in this rewrite and then will publish new version with this and maybe some other fixes. |
And, yeah :) Thanks :) I'm a bit angry now (obviously unrelated to this PR) so may be sounded a bit rude. Apologies for that. |
No worries. Looking forward to the rewrite. 🙂 |
This adds support for changing custom event handlers after the component has mounted. On every update of the component, the event handlers in the previous and current props are compared and (un)installed accordingly.
Not much of a react expert so looking forward to candid feedback. 🙂
Fixes: #156