Nested fragment nodes and correct implementation of mounDOM #237
Replies: 3 comments 1 reply
-
|
Hi there! This is a very good question, and indeed, that case is hard to comprehend. ExerciseI suggest you do the following exercise: add a console log inside the function createFragmentNodes(vdom, parentEl, index, hostComponent) {
const { children } = vdom
vdom.el = parentEl
children.forEach((child, i) => {
++ console.log( 'Mounting child', child, 'at index', index ? index + i : null)
mountDOM(child, parentEl, index ? index + i : null, hostComponent)
})
}Then, modify the <body>
<p>one</p>
<p>two</p>
<p>six</p>
</body>Now run the following code to mount a fragment that contains a fragment inside of itself: const vdom = hFragment([
h('p', {}, ['three']),
hFragment([h('p', {}, ['four']), h('p', {}, ['five'])]),
])
mountDOM(vdom, document.body, 2)You'll see the nodes are correctly mounted: <body>
<p>one</p>
<p>two</p>
<p>three</p>
<p>four</p>
<p>five</p>
<p>six</p>
</body>And in the console you'll see: ExplanationThe first call mounts the Then comes the internal fragment: Here's the key to understanding the result: this call to the mount function doesn't mount anything yet, as we have a fragment, we call the So we're inside the nested fragment now, and it's time for the And last goes So, the result is:
I hope this helps. Having said that, there might be something that I'm doing wrong myself. Best, |
Beta Was this translation helpful? Give feedback.
-
|
Hi! function createFragmentNodes(vdom, parentEl, index) {
const { children } = vdom
vdom.el = parentEl
children.forEach((child, i) =>
mountDOM(child, parentEl, index ? index + i : null)
)
}Let's consider the following example: const vdom = hFragment([
h("p", {}, ["three"]),
hFragment([
h("p", {}, ["four"]),
h("p", {}, ["five"])
])
])
mountDOM(vdom, document.body, 0);<body>
<p>one</p>
<p>two</p>
<p>six</p>
</body>Here, as the index argument is 0 for the mountDom function, in this line:
For its children the index becomes null and we get the following result for the HTML: <body>
<p>one</p>
<p>two</p>
<p>six</p>
<p>three</p>
<p>four</p>
<p>five</p>
</body>This change:
corrects it and we get: <body>
<p>three</p>
<p>four</p>
<p>five</p>
<p>one</p>
<p>two</p>
<p>six</p>
</body> |
Beta Was this translation helpful? Give feedback.
-
|
Also, let's see another example const vdom = hFragment([
hFragment([
h("p", {}, ["three"]),
h("p", {}, ["four"])
]),
h("p", {}, ["five"])
])
mountDOM(vdom, document.body, 2);Here, we have the nested fragment nodes in the first slot (that is difference with your example) We get the following result: <body>
<p>one</p>
<p>two</p>
<p>three</p>
<p>five</p> // those two are swapped
<p>four</p> // ----^
<p>six</p>
</body>I changed the createFragmentNodes function in this way and it corrected the result: function createFragmentNodes(vdom, parentEl, index) {
const { children } = vdom;
vdom.el = parentEl;
let index_count = index;
if (index === null) {
children.forEach((child, i) => mountDOM(child, parentEl, null));
} else {
for (let idx = 0; idx < children.length; idx++) {
console.log(index_count);
mountDOM(children[idx], parentEl, index_count);
if (children[idx].type == DOM_TYPES.FRAGMENT) {
index_count += extractChildren(children[idx]).length;
} else {
index_count += 1;
}
}
}
}The idea is expressed in the following picture below: |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have been trying to understand the source code for the version 2 of the framework. There we try to implement reconciliation algorithm and for that we have added some changes to the mountDOM function - namely, now it accepts the index at the parent element to mount the virtual DOM node to. I could not understand why this function is correct:
Here if we have the nested fragment nodes inside one another, then we have to change this line:
I think the final function would look like this:
Could anyone tell me what mistake am I making here?
Beta Was this translation helpful? Give feedback.
All reactions