GithubHelp home page GithubHelp logo

kazoogon / virtual_dom_from_scratch Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 14 KB

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

JavaScript 100.00%
react javascript virtualdom stack

virtual_dom_from_scratch's Introduction

๐Ÿ˜› 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 !!

virtual_dom_from_scratch's People

Contributors

kazoogon avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.