-
Notifications
You must be signed in to change notification settings - Fork 139
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
Unmount React component when disconnected. Closes #1260 #1432
Unmount React component when disconnected. Closes #1260 #1432
Conversation
I just commented on your issue and didn't realise there was a PR already! Fantastic! |
Happy for you to rely on
👍
Hmm, I don't remember and can't find anything on a quick search of issues. Would you mind reimplementing this? It should just be a copy pasta, which is okay for now. I'll leave this PR as is and await your response to discuss how we should proceed. |
Thanks for your review!
What's the best way to expose that in the test though? Should I use it with // in the test file
const el = new ReactComponentWrapper();
el.renderRoot = el; // expose like this?
I guess you'd want me to also use The reason I asked about |
29ef3c0
to
50da9ec
Compare
Right, so I cherry-picked the original change from the Preact renderer (to preserve authorship). I then tried to naively change all occurrences of |
packages/renderer-react/src/index.js
Outdated
@@ -1,6 +1,5 @@ | |||
import React from 'react'; | |||
import { render, unmountComponentAtNode } from 'react-dom'; | |||
import { withRenderer } from 'skatejs'; |
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.
This skatejs
import didn't seem to be used in this module, so I decided to remove it, along with the dependency on skatejs
in package.json
. But since withRenderer
from skatejs
was now being used in tests, I actually moved skatejs
to devDependencies
. Please let me know if you think removing this import is unsafe, e.g. if there's some ordering issue that this import was trying to work around, or if there's some side effects when skatejs
is imported
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.
Sounds good.
|
||
class MyElement extends withRenderer() { | ||
class MyElement extends withRenderer(withPreact()) { |
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.
I don't mind using withRenderer
here in the tests. On the surface it seems to simplify it, but I'm wondering why this didn't work before? It seems if you remove the dependency on withRenderer
and the accessing of renderRoot
, things would still work, no?
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.
You mean, just use the original code where root
was being stored on this._renderRoot
? I only updated it for consistency with the requested changes for React renderer really, but I'm happy to revert.
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.
Interestingly, the current tests still pass with the accessing of renderRoot
, would you rather I reverted these last changes to the Preact renderer tests? Or perhaps the accessing of renderRoot
too?
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.
I've reverted the renderRoot
changes to renderer-preact
, and left renderer-react
as is. Does make we wonder why not have the same implementation for the React renderer though 😛
9bd696b
to
a632f4e
Compare
Hey @NMinhNguyen I've updated the tests in c8b0006 to be as consistent as possible. Hopefully this simplifies the work you have to do. I don't think I originally grokked your intention and probably responded slightly out of context. |
@treshugart no worries. Just to clarify, what changes would you like me to make? Do you still want to use |
daf7280
to
d510ec4
Compare
Right, this is the part I misunderstood, apologies. I thought you meant in the tests. For the unmounting you can just store it in the underscore prefixed like the Preact renderer. Reusing Thanks again, and sorry for the run-around. |
@treshugart ah I think I might know where your confusion's coming from - it doesn't help that in the renderer tests, each renderer is called |
d510ec4
to
2de8a54
Compare
@@ -3,7 +3,8 @@ | |||
"browser": "dist/es/index.js", | |||
"description": "A SkateJS renderer for Preact.", | |||
"devDependencies": { | |||
"preact": "8.2.7" | |||
"preact": "8.2.7", | |||
"skatejs": "^5.1.2" |
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.
Added this since the tests are now pulling in skatejs
in c8b0006#diff-61ecfdc8a7d04b1bf1fbefe49953d7feR4
} | ||
} | ||
|
||
@define |
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.
made this test look more like c8b0006
disconnectedCallback() { | ||
super.disconnectedCallback && super.disconnectedCallback(); | ||
unmountComponentAtNode(this._renderRoot); | ||
this._renderRoot = null; |
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.
kinda got this habit from React where ref
s are nulled out when unmounting - prob a good idea to prevent any memory leaks. Could also add this line to the Preact renderer if you want?
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.
Yeah, that'd be great. I'll merge this to get it in and we can do that as a separate change. Should we test that it's nulled out you think?
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.
Sure, can do. It feels almost like testing an implementation detail (expect(el._renderRoot).toBeNull()
), but perhaps that's not so bad
Ready for another review. Will happily address any further comments. |
disconnectedCallback() { | ||
super.disconnectedCallback && super.disconnectedCallback(); | ||
// Preact hack https://github.com/developit/preact/issues/53 | ||
const Nothing = () => null; |
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.
I believe Preact will happily accept null
as the first argument to render()
these days, the component should no longer be needed.
Here's an example: https://jsfiddle.net/developit/xngr7pm1/
Closes #1260.
Requirements
contribution guidelines.
Rationale
Why is this PR necessary?
Implementation
Why have you implemented it this way? Did you try any other methods?
I based the implementation on skatejs/renderer-preact#5.
For the test, I looked at how React tests
unmountComponentAtNode
and how they spy on lifecycle hooks.Open questions
Are there any open questions about this implementation that need answers?
renderRoot
onthis._renderRoot
? I didn't want to rely onthis.renderRoot
being provided viawithRenderer
, but perhaps it's fine to requirewithReact
to always be used withwithRenderer
? It does mean that I'd either have to mock it in tests, or couple it towithRenderer
though.insideI've done some googling and seems like thedisconnectedCallback
, shouldsuper.disconnectedCallback()
be called first (the way it is in Make sure we notify preact in disconnectedCallback renderer-preact#5) or last?super
call is always first.renderer-preact
in the monorepo does not unmount, unlike the old repo?