Skip to content

๐Ÿ‘จโ€๐ŸŽจ create simple virtual DOM, like tiny React.js

Notifications You must be signed in to change notification settings

kazoogon/virtual_DOM_from_scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ˜› Build a Simple Virtual DOM from Scrath

To run

$ npm install
$ npm run dev

๐Ÿ—’ Side Notes

all variable with

  • $ - when referring to real doms, e.g $div, $el, $app
  • v - when referring to virtual doms, e.g vDiv, vEl, vApp

๐Ÿ“บ The way how to show elements on browser

1. createElement.js

just create object

โ–ผ like this plain object, this is virtual DOM
create_element

2.render.js

create actual DOM from virtual DOM object

โ–ผ like this <div id='app'></div>
render

3.mount.js

set actual DOM to target element in html file

โ–ผ then we can see on browser
Screenshot 2022-01-22 at 01 18 42

โ€ผ Problem

If I execute code which render every 1 sec, all elements re-render every sec as well
โ–ผ look at that, input renders every second, so I cant write anything !!! ๐Ÿ˜ฑ
render_all
everytime_render2

โ“ How to solve it

diff.js

1, find different node between old virtual DOM and new virtual DOM
2, return function the way how to change this node

โ–ผ now input doesnt render, so we can write normaly!! not_render_input
not_render2

๐ŸŒ Others

this algorithm of searching different node is "stack", this is not used in current React.js.
this "stack" has problem which need to search different node recursively from top node.

Now algorithm is called "Fiber". It is created by singly linked list, so it doesn't need to search different node recursively because each nodes are independent.

React Fiberใฎใ€ŒFiberใ€ใจใฏใชใ‚“ใชใฎใ‹ๅ‹‰ๅผทใ—ใฆใฟใŸ
A deep dive into React Fiber internals

๐Ÿ“• reference

VIDEO - Building a Simple Virtual DOM from Scratch - Jason Yu
This video helped me sooooooooo much !!

About

๐Ÿ‘จโ€๐ŸŽจ create simple virtual DOM, like tiny React.js

Topics

Resources

Stars

Watchers

Forks