Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/reactive-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ function reactiveElements(elementName, ReactComponent, options) {
if (useShadowDom) self.attachShadow({ mode: 'open' });

const observer = new MutationObserver(() => {
ReactDOM.unmountComponentAtNode(getRenderRoot(self, useShadowDom));
const props = utils.getProps(self);
props.children = utils.getChildren(self);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get rid of utils.getChildren now ? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's used in the initial render (where we do want it to be destructive, as we're about to move the DOM elements from the current DOM node they're in, to React for it to render)

I think very possibly getChildren as a name could stand to be improved, though, to make its destructive nature a bit clearer. Open to naming suggestions there, though

// we reuse the `childrenFragment`, rather than creating a new fragment,
// as this will cause a mutation in the current React component (because
// of the way that DOM nodes must be unique)
// but the fragment is a reference, so this is fine to keep a hold of
props.children = this.childrenFragment;

@ChristianMurphy ChristianMurphy Mar 6, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mutation observer also watches for changes to the slot/children/subtree.
Caching the childrenFragment will prevent updates that add children to the slot from being rendered.

create(self, props);
});

Expand All @@ -49,7 +52,9 @@ function reactiveElements(elementName, ReactComponent, options) {

connectedCallback() {
const props = utils.getProps(this);
props.children = utils.getChildren(this);
// save a `childrenFragment` to reuse later, on mutations (this is okay
// because the fragment is effectively a reference to the children)
this.childrenFragment = props.children = utils.getChildren(this);
let reactElement = create(this, props);

if (reactElement !== null) {
Expand Down