Skip to content

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

Merged
merged 2 commits into from
May 4, 2021

Conversation

Johennes
Copy link
Contributor

@Johennes Johennes commented May 2, 2021

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

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
Copy link
Owner

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.

@RIP21
Copy link
Owner

RIP21 commented May 3, 2021

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 :)

@Johennes
Copy link
Contributor Author

Johennes commented May 4, 2021

I had checked it in the demo and it appeared to work correctly with something like

  blur = () => console.log('blur')

  render() {
    return (
      ...
        <SimpleMDEReact
          ...
          events={{
            'blur': this.blur,
          }}
        />

but not with something like

  render() {
    return (
      ...
        <SimpleMDEReact
          ...
          events={{
            'blur': () => console.log('blur')
          }}
        />

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 this.props. I updated the demo to showcase that it works (happy to remove that part again).

@RIP21
Copy link
Owner

RIP21 commented May 4, 2021

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.

@RIP21 RIP21 merged commit 8105e69 into RIP21:master May 4, 2021
@RIP21
Copy link
Owner

RIP21 commented May 4, 2021

And, yeah :) Thanks :) I'm a bit angry now (obviously unrelated to this PR) so may be sounded a bit rude. Apologies for that.

@Johennes
Copy link
Contributor Author

Johennes commented May 5, 2021

No worries. Looking forward to the rewrite. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Event handlers are only installed once but never updated
2 participants