Browser operating principles + React principles

- Creating DOM(Document Object Model) - Lifecycle Method: DOMContentLoaded, load, beforeunload, unload
 - DOMContentLoaded- Occurs immediately after completion of the DOM tree.
- No Wait for assets like img, css etc
 
 - <script>
 function ready() {
 alert("DOM is ready!");
 // size is 0X0 as it is not loaded
 alert(`img size: ${img.offsetWidth}x${img.offsetHeight}`);
 }
 document.addEventListener("DOMContentLoaded", ready);
 </script>
 <img id="img" src="https://en.js.cx/clipart/train.gif?speed=1&cache=0" />- If multiple script tags exist => DOMCcontentLoaded runs after all scripts run (reason: DOMcontentLoaded may have logic related to DOM manipulation)
 - <!-- b => c => a -->
 <script>
 document.addEventListener("DOMContentLoaded", () => {
 alert("DOM is ready!");
 });
 // a
 </script>
 <script
 src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"
 >
 // b
 </script>
 <script>
 // c
 alert("๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ์ด ๋๋๊ณ ์ธ๋ผ์ธ ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋์์ต๋๋ค.");
 </script>- Scripts created dynamically with document.createElement ('script')- and added to web pages do not prevent DOMCentLoaded.
- if script location after CSS loading link tag - Script tag will not run until CSS loading is complete - Therefore, DOMCcontentLoaded runs after style loading too (reason: Possible use of style-affected properties during JS like img, offsetTopโฆ)
 
 - load => Executed when all external resources such as img and CSS are loaded
- beforeunload / => Occurs when a user leaves the page !https://blog.kakaocdn.net/dn/bwVtza/btrMI23XIp7/f7LpqlCEGumD6kfIPvEsg0/img.png
- unload (the last thing to do when you really leave) => Occurs when a user leaves the page
 - // only document
 window.addEventListener("DOMContentLoaded", (event) => {
 console.log("DOMContentLoaded");
 });
 // after resources (css, images)
 window.addEventListener("load", (event) => {
 console.log("load");
 });
 // before unload
 window.addEventListener("beforeunload", (event) => {
 console.log("beforeunload");
 });
 // resource is being unloaded
 window.addEventListener("unload", (event) => {
 console.log("unload");
 });
- Creating CSSOM(Cascade Style Sheet Object Model) - DOM + CSS = CSSOM
=> DOM + CSSOM = Render Tree
 
- display:noneapplied to DOM => including to Final Render Tree
- visibility: hidden / opacity: 0=> including to Final Render Tree(Element exists, but not visible) 
 - Parsing HTML, CSS, JS(= Synchronous Parsing)โ- at facing tag while parsing HTML โ simultaneously parsing HTML, CSS - => CSS doesnโt change DOM structure 
- JS can change DOM structure, then if you face script tag, then stop parsing HTML parse and start JS parse 
 - No blocking if you use defer, async(= Asynchronous Parsing)โ- async - JS assets is downloaded at background environment
- JS assets is loaded async, JS parsing is after HTML parsing- JS runs with HTML parsing pause
 
- if thereโs multiple async, running order is on itโs own terms
 - <script
 async
 src="https://javascript.info/article/script-async-defer/long.js"
 ></script>
 <script
 async
 src="https://javascript.info/article/script-async-defer/small.js"
 ></script>
 <!-- small.js is run first before long.js -->- DOMContentLoadedย event, async script is not waiting each other.
 - If the async script is downloaded after the page is configured, DOMCcontentLoaded may occur before the async script is executed,
- If the async script is short and is downloaded before the page configuration is finished, or if the script is cached, DOMCcontentLoaded can also occur after the async script is executed.
 
- defer - JS download runs in the background
- JS load is asynchronous, JS execution is synchronized (HTML parsing is completed)
- Executed before the DOMCcontentLoaded event occurs
 - <!-- a -->
 <p>...content after script...</p>
 <!-- b -->
 <script
 defer
 src="https://javascript.info/article/script-async-defer/long.js?speed=1"
 ></script>
 <!-- see now! -->
 <!-- c -->
 <p>...script after content...</p>
 <!-- order a => c => b -->- The above script runs and the below script runs (small.js, a small script, is download before long.js, a long script)
 - <!-- a -->
 <script
 defer
 src="https://javascript.info/article/script-async-defer/long.js"
 ></script>
 <!-- b -->
 <script
 defer
 src="https://javascript.info/article/script-async-defer/small.js"
 ></script>
 <!-- b runs after a is completed -->- if src in script is not exists, then defer property is ignored(inline script tag is ignored)
 - <!-- defer is not applied -->
 <script defer>
 document.addEventListener("DOMContentLoaded", () =>
 alert("DOM is ready after running `defer`script")
 ); // (2)
 </script>
 <!-- defer is applied -->
 <script
 defer
 src="https://javascript.info/article/script-async-defer/long.js?speed=1"
 ></script>
 
- DOM + CSS = CSSOM
=> DOM + CSSOM = Render Tree
- Creating Render Tree = DOM + CSSOM 
- Run โLayoutโ process - Each node in the render tree => Calculate which viewport and where it should be placed- Relative properties such as %, vh, vw are converted to px
 
 
- Each node in the render tree => Calculate which viewport and where it should be placed
- Run โPaintโ process - draw each node in the render tree as a real pixel on the monitor- When reflow occurs, paint must occurHowever, if there are properties that affect the layout, such as background-color, visibility, etc., do not need to reflow => Therefore, only paint is performed 
 
- When reflow occurs, paint must occur
- Property that neither Layout nor Paint occurs- transform, opacitiy, cursor, orphans, perspective etc
- Rendering is faster than the 'Paint Only' property (absolutely reduced operations)=> If possible, it is recommended to use properties that do not cause Repaint or Reflow. 
- Different rendering engines in your browser(chrome, safari, firefox etc) might have different steps to process CSS properties
 
 
- draw each node in the render tree as a real pixel on the monitor
๊ฐ์๋(Virtual DOM) ํน์งโ
- ํ์ฌ ๋์ ๋ณต์ฌ๋ณธ์ธ ๊ฐ์๋ A์, ๋ณ๊ฒฝ์ด ์๋ ๊ฐ์๋ B(๋ฉ๋ชจ๋ฆฌ์ ์๋ก ์์ฑ)๋ฅผ ๋น๊ต(ํด๋ฆฌ์คํฑ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋น๊ต) => ์ฐจ์ด์ ์ ๋ชจ๋ ๋ถ๋ถ์ ์ผ๊ด์ ์ผ๋ก, ๋ฆฌ์กํธ๊ฐ ์ค์  DOM์ ๋ฐ์ํ๋ค
- ๋ฐฐ์น(batch) => DOM ์ ๋ฐ์ดํธ๋ฅผ ์ผ๊ด์ ์ผ๋ก ์ฒ๋ฆฌ => ์ค์  ๋์ ๋ฆฌ๋ ๋๋ง ์ฐ์ฐ์ ์ต์ํ(์ฐ์์  ๋ฆฌํ๋ก์ฐ, ๋ฆฌํ์ธํธ ๋ฐ์ ๋ง์)
์์ ๊ท๋ชจ์ ๋ ์ด์์(๋ฆฌํ๋ก์ฐ)์ด ์ฌ๋ฌ๋ฒ ๋ฐ์ํ๋ ๊ฒ๋ณด๋คโ
ํฐ ๊ท๋ชจ์ ๋ ์ด์์์ด ํ ๋ฒ ๋ฐ์ํ๋ ๊ฒ์ ์ฑ๋ฅ์์ ํฐ ์ฐจ์ด๋ฅผ ๋ํ๋โ

์ ๋ฐ์ดํธ ๋ ๊ฐ์๋๊ณผ ์ด์  ๊ฐ์๋์ ๋น๊ต
์ฌ์กฐ์ (Reconciliation)
- ๋ค์ ๊ฐ์ ์ ํตํด => ์ ์ฒด DOM ํธ๋ฆฌ๋ฅผ ํ์ํ๋ ์๊ฐ์(O(n^3))์์ O(n)์ผ๋ก ์ค์ - ์๋ก ๋ค๋ฅธ ํ์ ์ ๋ ์๋ฆฌ๋จผํธ๋ ์๋ก ๋ค๋ฅธ ํธ๋ฆฌ๋ฅผ ๋ง๋ค์ด๋(์๋ฆฌ๋จผํธ๊ฐ ๋ค๋ฅด๋ฉด ๋น๊ต ์ํจ)
- key props๋ฅผ ํตํด => key๊ฐ ๊ฐ์ ๋ ธ๋๋ผ๋ฆฌ ๋น๊ตํจ
 
- ๋น๊ต ๋ฐฉ์ - ๋์ผํ ๋ ๋ฒจ์ ๋ ธ๋๋ค๋ผ๋ฆฌ๋ง ๋น๊ต
- ๊ฐ์ ์์น์์ ์๋ฆฌ๋จผํธ ํ์ ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ => ๊ธฐ์กด ํธ๋ฆฌ๋ฅผ ์ ๊ฑฐํ๊ณ ์๋กญ๊ฒ ํธ๋ฆฌ๋ฅผ ๋ง๋ ๋ค(๋ด๋ถ ์๋ฆฌ๋จผํธ, ๋ด๋ถ ์ปดํฌ๋ํธ ๋ชจ๋ ์ ๊ฑฐ ํ ๋ค์ ์๋ก ๋ง๋ฌ)
- ๊ฐ์ ์์น์์ ์๋ฆฌ๋จผํธ์ ํ์ ์ด ๊ฐ์ ๊ฒฝ์ฐ => class๊ฐ ๋ณ๊ฒฝ๋๋ค๋ฉด ๋ณ๊ฒฝ๋ attributes๋ง ์ ๋ฐ์ดํธํ๋ค => ์์ ์๋ฆฌ๋จผํธ๋ค์ ๋น๊ต์๊ณ ๋ฆฌ์ฆ์ ์ฌ๊ท์ ์ผ๋ก ์ ์ฉ
- ๊ฐ์ ์์น์์ '์๋ฆฌ๋จผํธ = ์ปดํฌ๋ํธ'์ด๊ณ , ํ์ ์ด ๊ฐ์๊ฒฝ์ฐ => ์ปดํฌ๋ํธ ์ธ์คํดํธ ์์ฒด๋ ์๋ณํจ(state ์ ์ง) + ๋ผ์ดํ์ฌ์ดํด ๋ฉ์๋๋ฅผ ์ด์ฉํด props๊ฐ ์ ๋ฐ์ดํธ๋จ => render ํจ์๋ฅผ ํธ์ถํด์ ์ปดํฌ๋ํธ ์ด์  ์๋ฆฌ๋จผํธ ํธ๋ฆฌ์ ๋ค์ ์๋ฆฌ๋จผํธ ํธ๋ฆฌ์ ๋ํด ์ฌ๊ท์ ์ผ๋ก ์ ์ฉ
- ์์ ๋
ธ๋์ ๋ํ ์ฌ๊ท์  ์ฒ๋ฆฌ ์- => key๋ฅผ ์ด์ฉํด ๋์ผํ key๋ฅผ ๊ฐ๋ ์์๋ค๋ผ๋ฆฌ๋ง ๋น๊ตํ๋ค => ๋ฐ๋ผ์ ItemC ๋ ธ๋๋ง ์ถ๊ฐ๋๋ค. (key๊ฐ ์์ผ๋ฉด ๋๋ฆฌ์คํธ๋ฅผ ๋ชจ๋ ์ํํด์ผํจ)
- => ์ธ๋ฑ์ค๋ฅผ key๋ก ํ ๊ฒฝ์ฐ =>ย ๋ฆฌ์คํธ์ ๋ฐฐ์ด์ด ์ฌ์ ๋ ฌ๋์ง ์๊ฑฐ๋ last-child์์๋ง ์ถ๊ฐ, ๋ณ๊ฒฝ, ์ ๊ฑฐ๊ฐ ์ผ์ด๋๋ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ํค๋ก ์ฌ์ฉํด๋๋จ
- => ํ์ง๋ง ๋ฆฌ์คํธ ์์๊ฐ ๋ฐ๋๋ฉด key๊ฐ ์ ๋ถ ๋ฐ๋์ด์ key์ ์๋ฏธ๊ฐ ์ฌ๋ผ์ง
 
 - // before
 <ul>
 <li>ItemA</li>
 //๋น๊ต๋์1
 <li>ItemB</li>
 //๋น๊ต๋์2
 </ul>
 // after
 <ul>
 <li>ItemC</li>
 //๋น๊ต๋์1 (ItemA -> ItemC ๋ก ๋ณ๊ฒฝ)
 <li>ItemA</li>
 //๋น๊ต๋์2 (ItemB -> ItemA ๋ก ๋ณ๊ฒฝ)
 <li>ItemB</li>
 //๋น๊ต๋์3 (ItemB ์ถ๊ฐ)
 </ul>

๋ ๋๋ง ํธ๋ฆฌ ๋น๊ต
์ฐธ๊ณ
[React] Virtual DOM๊ณผ ๋ธ๋ผ์ฐ์ ์ ๋ ๋๋ง ๊ณผ์ 
๋ธ๋ผ์ฐ์ ์ ๋์๊ณผ์ ๋ถํฐ React๊น์ง
[React] ๊ฐ์๋ Virtual DOM์ด๋?