pomber / didact

A DIY guide to build your own React
https://pomb.us/build-your-own-react/
6.29k stars 531 forks source link

Updated child element gets appended to the end of the parent node. #27

Open ashishtayal89 opened 4 years ago

ashishtayal89 commented 4 years ago

If my updated element is not the last child, it is pushed to the last due to appendChild. Although we delete the updated element's dom from the parent dom, we are still appending the updated element's dom to the end.

Eg `

Chapter 4 (Reconciliation)

  {update ? <span id="span1">span1</span> : <div id="div1">div1</div>}
  <div id="div2">div2</div>
 <div>

` In the above case, the span gets appended as the last child of the parent div instead of being the 2nd child when the update becomes true. Should we be using "repalceChild" instead?

BulingBulingWei commented 2 years ago

I got the same problem here! When append child, we must consider where to place the child