<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shunze on Medium]]></title>
        <description><![CDATA[Stories by Shunze on Medium]]></description>
        <link>https://medium.com/@4a490101?source=rss-89369fd5c0a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qine8ox1-ND4n3CxcYMY0Q.jpeg</url>
            <title>Stories by Shunze on Medium</title>
            <link>https://medium.com/@4a490101?source=rss-89369fd5c0a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:58:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@4a490101/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[【JavaScript】Asynchronous JavaScript]]></title>
            <link>https://medium.com/shunze0925/javascript-asynchronous-javascript-772344251888?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/772344251888</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Thu, 04 Mar 2021 14:55:49 GMT</pubDate>
            <atom:updated>2021-03-04T14:55:49.940Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Synchronous</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*CPclTA4h-zP0AGMF8GgYmA.png" /></figure><p>Synchronous means the code is executed line by line, and each line of code waits for previous line to finish.</p><p>But if some code will take some time to finish, it might block the thread, so we need a way to solve this problem.</p><h3><strong>Asynchronous</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*Y85yENo3ehLzGkCR7IaFLw.png" /></figure><p>Asynchronous code like timer run in the background and register a callback function, the callback run after the timer has finished.</p><p>And the <strong>main code is not being blocked</strong>, and execution doesn’t wait for the asynchronous timer to finish its work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*9IVi3GRp3L2KYTcMyA8PtA.png" /></figure><p>Image is also loading in the background, once the image has finished loading, a load event will be emitted by JavaScript.</p><p>And finally the callback function is executed.</p><h3>AJAX</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*mxTy6rbqAtSWdGw5b6g4Yg.png" /></figure><p>AJAX allows us to communicate with remote web servers in a <strong>asynchronous way</strong>.</p><p>In practice, we make AJAX calls in our code in order to request some data from a web server dynamically.</p><h3>How AJAX work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*B5CwILf1K1Wu75t5OVEDFQ.png" /></figure><p>Our JavaScript application running in the browser, which is also called <strong>Client</strong>.</p><p>With AJAX, we can do HTTP request to the server which has the data we want to fetch.</p><p>And the server will then send back a response contains the data we requested.</p><p>And the process between Client and Server all happens asynchronously in the background.</p><h3>API</h3><p>When we’re asking a server to send us some data, the server usually contains a web API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*S6tB3LB70E2z7-aUM0aDiw.png" /></figure><p>API is basically a piece of software that can be used by another piece of software in order to allow applications to talk to each other and exchange information.</p><p>There are many types of APIs, but the most important type of API can be called <strong>Online API </strong>or just<strong> API</strong>.</p><p>Online API is an application running on a web server, which receives request for data, then retrieves the data from some database, and sends back to the Client.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=772344251888" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/javascript-asynchronous-javascript-772344251888">【JavaScript】Asynchronous JavaScript</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript in the Browser: DOM]]></title>
            <link>https://medium.com/shunze0925/javascript-in-the-browser-dom-fa261f72f43b?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa261f72f43b</guid>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Sat, 13 Feb 2021 13:03:12 GMT</pubDate>
            <atom:updated>2021-02-13T14:49:41.028Z</atom:updated>
            <content:encoded><![CDATA[<h3>【JavaScript】JavaScript in the Browser: DOM</h3><blockquote>The actual contents comes from The Complete JavaScript Course 2021.</blockquote><blockquote>If you have interests, you can check <a href="https://www.udemy.com/course/the-complete-javascript-course/">The Complete JavaScript Course</a> by <strong>Jonas Schmedtmann.</strong></blockquote><h3><strong>What is DOM?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*P63364v1avoFWVBpGqb-7Q.png" /></figure><p>It’s structured representation of HTML documents.</p><p>Allows JavaScript to access HTML elements and styles to manipulate them.</p><p>So we can say DOM is a <strong>connection point</strong> between <strong>HTML documents</strong> and <strong>JavaScript code</strong>.</p><h3><strong>When does DOM create?</strong></h3><p>The DOM is automatically created by the browser as soon as the HTML page loads.</p><p>And it’s stored in a tree structure like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*EeJJOqXzYWpYmQSaUN-05w.png" /></figure><p>In the tree, each HTML element is one <strong>object</strong>.</p><h3><strong>DOM Structure in Detail</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*7KA1wvzfV7BJjYGdy-HdQg.png" /></figure><p>For each element in the HTML, there’s one element node in the DOM tree.</p><p>And we can access and interact with each of these nodes using JavaScript.</p><h3><strong>What’s on the top of the DOM?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*hJlJOTZud7UGylUbRb6w5A.png" /></figure><p>The DOM starts with the <strong>document object</strong>, and this object we have access to in JavaScript.</p><p>This object serves as an <strong>entry point</strong> into the DOM.</p><h3><strong>What’s the first Child Elements of document?</strong></h3><p>The first child element is the <strong>HTML element</strong>, it’s the root element in all HTML documents.</p><h3><strong>What’s two child elements of HTML element?</strong></h3><p>It’s <strong>head</strong> and <strong>body</strong>.</p><h3><strong>What else does DOM tree have?</strong></h3><p>It also has nodes for all the text itself, comments and other stuff.</p><p>Whatever is in the HTML document also has to be in the DOM.</p><p>So the DOM is a complete representation of the HTML document.</p><h3><strong>The Truth about DOM</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*Vrb9hzekOl5DuIxvJ2yBMA.png" /></figure><p>DOM is not a part of the JavaScript language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*aDNNA6X748bw6N7kO5ew1Q.png" /></figure><p><strong>DOM</strong> and <strong>DOM methods</strong> are part of something called <strong>Web APIs</strong>.</p><p>Web APIs are like libraries that browsers implement and that we can access and use from our JavaScript code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa261f72f43b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/javascript-in-the-browser-dom-fa261f72f43b">JavaScript in the Browser: DOM</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[【JavaScript】Execution Context and CallStack]]></title>
            <link>https://medium.com/shunze0925/javascript-execution-context-and-callstack-d9f52ffd81e9?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9f52ffd81e9</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Mon, 08 Feb 2021 14:50:12 GMT</pubDate>
            <atom:updated>2021-02-08T14:50:59.843Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The actual contents comes from The Complete JavaScript Course 2021.</blockquote><blockquote>If you have interests, you can check <a href="https://www.udemy.com/course/the-complete-javascript-course/">The Complete JavaScript Course</a> by <strong>Jonas Schmedtmann.</strong></blockquote><h3><strong>After Compiling, Execution in Detail</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*ZZ2FdZdgx-dmuRH8q23Idw.png" /></figure><ol><li>Global Execution Context is created for the <strong>top-level</strong> code.</li><li>Top-Level code be executed.</li><li>After Top-Level code is finished, functions start to execute.</li></ol><h3><strong>What is top-level Code?</strong></h3><p>It’s code that is <strong>not</strong> <strong>inside any function</strong>, in the beginning only the code that is outside of functions will be executed.</p><h3><strong>What is Execution Context?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/418/1*j4Y6ME4vnvaWJqzrAoFvgw.png" /></figure><p>JavaScript code always runs inside an execution context.</p><h3><strong>Only One Global Execution Context</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*LBTJgkk8ltaLeWjZzOjixA.png" /></figure><h3><strong>One Execution Context — One Function</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*y6_uYSnhj3HbULu9n5TXug.png" /></figure><h3><strong>What’s inside Execution Context?</strong></h3><ol><li>Variable Environment</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*GcaQoMZyZVd2FA-WFp1khQ.png" /></figure><p>All our <strong>variables</strong> and <strong>function declarations</strong> are stored, and there’s also a special <strong>arguments object</strong>.</p><h3><strong>What’s inside the Arguments Object?</strong></h3><p>Argument object contains <strong>all the arguments that were passed into the function</strong> that the current execution context belongs to.</p><p>2. Scope</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*9IegmKoDeDxJyyIoaKBnrA.png" /></figure><p>Lexical scope consists of references to variables that are located outside of the current function.</p><p>3. this keyword</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*buO-bUZjv9F2WXQ86aK6wg.png" /></figure><h3><strong>Creation Phase</strong></h3><p>The content of the execution context, <strong>variable environment</strong>, <strong>scope</strong>, and <strong>this keyword</strong> is generated in so-called <strong>Creation Phase</strong>.</p><p>Which happens right <strong>before execution</strong>.</p><h3><strong>What is Call Stack?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*vwPCRTZQcUvagrUwIy9UMQ.png" /></figure><p>The execution context that is on top of the stack, is the one that is currently running.</p><p>When it finishes running, it will be removed from the stack and execution will go back to the previous execution context.</p><h3><strong>How Call Stack Work?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*FiLfAW0D-jnpcXcn_g12yw.png" /></figure><p>1. Top-Level code start execution, and Global Execution Context created for the Top-Level code.</p><p>2. Global Execution Context be put in the Call Stack.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*yQf1fOc2klbBwEJFpwoWug.png" /></figure><p>3. When a function is called, it gets its own Execution Context, and put in the call stack, on top of the current context, now it’s the new current Execution Context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*U8MKBNzfo_7yp5tq3mPCEA.png" /></figure><p>4. The function below the current Execution Context will be paused.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*Y5iTFinuVt964jIV1794ug.png" /></figure><p>5. When a function return, meaning it will finish its execution, so the function’s Execution Context, will be popped off the stack.</p><p>6. The previous Execution Context, will be back to being the current Execution Context.</p><p>7. If the program only left with Global Execution Context, it will stay in this state until the app is eventually finished, then Global Execution Context popped off the stack.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9f52ffd81e9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/javascript-execution-context-and-callstack-d9f52ffd81e9">【JavaScript】Execution Context and CallStack</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[【JavaScript】JavaScript Engine and Runtime]]></title>
            <link>https://medium.com/shunze0925/javascript-javascript-engine-and-runtime-538a610ef425?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/538a610ef425</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Sun, 07 Feb 2021 08:37:37 GMT</pubDate>
            <atom:updated>2021-02-07T08:39:32.344Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The actual contents comes from The Complete JavaScript Course 2021.</blockquote><blockquote>If you have interests, you can check <a href="https://www.udemy.com/course/the-complete-javascript-course/">The Complete JavaScript Course</a> by <strong>Jonas Schmedtmann.</strong></blockquote><h3>JavaScript Engine</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*PwpPV5DM4TJvLD-bQDsGbg.png" /></figure><p>JavaScript Engine is a computer program that executes JavaScript code.</p><p>Every browser has its own JavaScript engine, the most well-known engine is <strong>Google V8</strong>.</p><h3><strong>Call Stack and Heap</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*SL8RDmgj14wH7aZMMi2uqg.png" /></figure><p>The call stack is where our code is actually executed, using something called <strong>execution contexts</strong>.</p><p>The heap is an unstructured memory pool, which stores all the <strong>objects</strong> that our application needs.</p><h3><strong>Compilation vs Interpretation</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*3UTJwBjI7xq610ugn8BcCg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*QltsltKExC_00EydVDi-6Q.png" /></figure><h3><strong>Just-In-Time Compilation</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*ckXTRUl26ZzGBT43htSxgg.png" /></figure><p>Modern JavaScript engine use a mix between compilation and interpretation, which is called <strong>just-in-time compilation</strong>.</p><p>This approach compiles the entire code into machine code at once, and then executes it right away.</p><p>The execution happens immediately after a compilation.</p><h3><strong>Just-In-Time Compilation in Detail</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*ItIvB_niUZwdn4-xdPMPdA.png" /></figure><ol><li>Parse the code, which means to read the code, and the code is parsed into a data structure called <strong>AST</strong>(Abstract Syntax Tree).</li><li>Takes the generated AST and compiles it into machine code.</li><li>The machine code then gets executed right away.</li><li>The first unoptimized code is being optimized and recompiled during the already running program execution, this process can be done multiple time.</li></ol><h3><strong>How AST Works?</strong></h3><p>This works by first splitting up each line of code into pieces that are meaningful to the language, like the const or function keywords, and then saving all these pieces into the tree in a structured way.</p><p>This step also checks if there’re any <strong>syntax errors</strong>.</p><h3><strong>JavaScript Runtime (Browsers)</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*DL8LWzmLGLX8Krn1YZ0bNA.png" /></figure><p><strong>-Web APIs</strong> are functionalities provided to the engine, but are <strong>not</strong> part of the JavaScript language itself.</p><p>JavaScript gets access to these APIs through <strong>global window object</strong>.</p><p><strong>-Callback queue</strong>, is a data structure that contains all the <strong>callback functions</strong> that are <strong>ready to be executed</strong>.</p><p><strong>-Event Loop</strong>, takes callback functions from the callback queue and puts them in the <strong>call stack</strong>, so that they can be executed.</p><h3><strong>JavaScript Runtime (Node.Js)</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*kgHUkYN0nddwwDWTKH9hhA.png" /></figure><p>Since we don’t have a browser, we don’t have WEB APIs because it’s the browsers who provide these.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=538a610ef425" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/javascript-javascript-engine-and-runtime-538a610ef425">【JavaScript】JavaScript Engine and Runtime</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Router v5]]></title>
            <link>https://medium.com/shunze0925/react-router-v5-51001e12beeb?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/51001e12beeb</guid>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Sat, 06 Feb 2021 01:06:51 GMT</pubDate>
            <atom:updated>2021-02-06T14:44:08.265Z</atom:updated>
            <content:encoded><![CDATA[<h3>【React】React Router v5</h3><blockquote>The actual contents comes from UI.DEV React Router v5.</blockquote><blockquote>If you have interests, you can check <a href="https://ui.dev/react-router-v5/">React Router v5</a> by Tyler McGinnis.</blockquote><h3><strong>What’s the mental model of React Router?</strong></h3><p>Route is gonna either render the children component if matched or it’s gonna render null.</p><p>And Route is just another component, we can render routes anywhere inside of our application.</p><h3><strong>Nested Links</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*AmdPNR279lmnOzto0vw-8w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/142/1*MQ4dRSbCkg33mETTfEdulg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*4bw4qjpxQPxDCObKhJUUdA.png" /></figure><p>If building nested links, we can use <strong>useRouteMatch</strong> hooks, it’ll return an object contains current URL pathname property.</p><p>Then we can use it to set a dynamic link rather than sticky one.</p><h3><strong>Nested Routes</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/267/1*39QmZ3trdB2QZvQGckCuig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*OBtAiaUimz7jNSuqn6fRUg.png" /></figure><p>useRouteMatch also return a path property, which contains current pathname preserved params.</p><p>So if we have a nested route, we can use path property to set the path of Nested Route.</p><h3><strong>Passing Props — Router v4 &amp; v5</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*CInnGgsudvfJZyd0p9YKTw.png" /></figure><p>They both pass props into component, but in v5, we don’t have to use complicated syntax.</p><p>The only thing we need to do is to wrap our component and pass the props we want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*eWm191TyrQE11gDpnIRTEA.png" /></figure><p>With Redirect, we have a declarative way to navigate rather than imperative way (history.push).</p><h3><strong>Query String</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/235/1*HBCaM4Pc4hvf2-Nx7rfHgg.png" /></figure><p>With React Router, we can grab the query string from the URL by using <strong>useLocation</strong> hooks, it’s gonna have a search property in location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*jueoEbtEYYnzb1GbAWmHIQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*eJIAEXsQTRAHPBlB26FzkQ.png" /></figure><p>And then we can convert the string to actual value by using query string package or built-in URL search Params which comes with the browser.</p><h3><strong>404 Route</strong></h3><p>If the user come to the non-existed route, rather than showing a white screen to user, we need a component to tell user “not found” information.</p><p>By using an universal path (*), it’ll match any pattern.</p><p>So all we need to do is put the universal route to last one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*RgGky7ivX3OG0jMlZMR8LQ.png" /></figure><p>Inside of a switch, it will only render the first route that matches, if no any component match, then it will render 404 Route Component.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=51001e12beeb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/react-router-v5-51001e12beeb">React Router v5</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[【React】React Performance Note]]></title>
            <link>https://medium.com/shunze0925/react-react-performance-note-79db310d56de?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/79db310d56de</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-performance]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 08:40:34 GMT</pubDate>
            <atom:updated>2021-02-06T14:43:45.687Z</atom:updated>
            <content:encoded><![CDATA[<h3>【React】React Performance</h3><blockquote>The actual contents comes from Epic React’s Advanced React Patterns Workshop.</blockquote><blockquote>If you have interests, you can check <a href="https://epicreact.dev/">Epic React</a> by Kent C. Dodds.</blockquote><h3><strong>Code Splitting (Dynamic Import)</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*XeUVo6MMxCL-LRKgFdm98Q.png" /></figure><p>The module must have a <strong>React component</strong> as the <strong>default export</strong>, and we have to use the &lt;<strong>React.Suspense</strong> /&gt; component to render a fallback value while the user waits for the module to be loaded.</p><h3><strong>Load when Event Happened</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*YC6n8rwgaOfGdAUjE5ZaKQ.png" /></figure><p>We can also trigger an event to load the module we want.</p><h3><strong>webpack Magic Comments</strong></h3><p>If we’re using webpack to bundle, then we can use <a href="https://webpack.js.org/api/module-methods/#magic-comments">magic comments</a> to have webpack <strong>instruct the browser to prefetch dynamic imports:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*zEwOxXlcG5vCe9N8FNhRWg.png" /></figure><p>Then webpack will adds this to our document’s head:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*PhXfTkf8lkPP99jLxAx6Iw.png" /></figure><p>Then the browser will automatically load this JavaScript file into the <strong>browser cache </strong>as soon as the browser no longer busy.</p><h3><strong>webpackChunkName</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*wvt_4hQnQsYRiTtemJDa2Q.png" /></figure><p>By using this, which will allow webpack to place <strong>common modules in the same chunk</strong>.</p><p>This is good for components which we want loaded together in the same chunk (to reduce multiple requests for multiple modules which will likely be needed together).</p><h3><strong>Web Workers</strong></h3><p>Web Workers are a browser standard that enables us to let browser spin up multiple threads to run different JavaScript files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*wl4Z9llh6Fn4-0R3odN3iQ.png" /></figure><p>If some JavaScript that’s expensive to run, we can put it on the separate thread, so our main thread is more rapidly to user.</p><h3><strong>workerize-loader</strong></h3><p>It is a webpack loader for workerize which means we can put any module (and the modules that it imports) into a webworker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*Blb8rVj6M8kJWl_uyFAF0Q.png" /></figure><p>The workerize! thing in the import statement is a webpack syntax to tell webpack to treat that module specially to pipe it through the workerize-loader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*GJkzxPWVJue5rZoDVrdZ5Q.png" /></figure><p>The communication between Webworker and the browser’s main thread is <strong>asynchronous</strong>.</p><h3><strong>Performance with Large List</strong></h3><p>React is really optimized at updating the DOM during the commit phase.</p><p>Unfortunately, there’s not much React can do if you simply need to make <strong>huge updates to the DOM</strong>.</p><p>These problems come with than dataviz, grids, tables, and <strong>lists with lots of data</strong>.</p><h3><strong>Lazy Just-In-Time Rendering</strong></h3><p>The main idea is that we don’t need to actually display tens of thousands of list items, table cells, or data points to users.</p><p>If that <strong>content isn’t displayed</strong>, then we can doing some <strong>“lazy” just-in-time rendering</strong>.</p><h3><strong>react-virtual</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*O5eN7vGZmIIHnpYx6gBoFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*jSTD9ABqcvoLPXCHZ8W6Dg.png" /></figure><p>Rather than iterating over all the items in your list, we tell useVirtual <strong>how many rows are in your list</strong>, give it a callback to <strong>determine what size they each should be</strong>, and then it will give us <strong>virtualItems</strong> and <strong>totalSize</strong> which we can then use to only <strong>render the items the user should be able to see within the window</strong>.</p><h3><strong>Context System</strong></h3><p>The way that context works is that whenever the <strong>provided value changes</strong> from one render to another, it <strong>triggers a re-render of all the consuming components</strong> (which will <strong>re-render whether or not they’re memoized</strong>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*v2vtJtkfUznfxRnYXj-_qQ.png" /></figure><p>Every time the &lt;CountProvider /&gt; is re-rendered, the <strong>value is brand new</strong>, so even though the count value itself may stay the same, <strong>all component consumers will be re-rendered</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*8se-Zw-tPHxlnCpEKwqUQQ.png" /></figure><p>The easy solution is to <strong>memoize the value provided to the context provider</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79db310d56de" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/react-react-performance-note-79db310d56de">【React】React Performance Note</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Advanced React Patterns Note]]></title>
            <link>https://medium.com/shunze0925/advanced-react-patterns-note-a0fcad56e43b?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0fcad56e43b</guid>
            <category><![CDATA[react-advanced-patterns]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 08:25:48 GMT</pubDate>
            <atom:updated>2021-02-05T10:14:20.775Z</atom:updated>
            <content:encoded><![CDATA[<h3>【React】Advanced React Patterns</h3><blockquote>The actual contents comes from Epic React’s Advanced React Patterns Workshop.</blockquote><blockquote>If you have interests, you can check <a href="https://epicreact.dev/">Epic React</a> by Kent C. Dodds.</blockquote><h3><strong>Context Module Functions</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*w3cr0Bb753xIljjMJmpmZg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*Pi6JPlOWftQPVlJETuYzBw.png" /></figure><p>The benefit of this is if we have to call multiple dispatch, if we leave them to let user to call, they might miss a dispatch call, so it’s better to pass the dispatch to the context module function.</p><h3><strong>Compound Components</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*G6VlyWrp54UQYF_sTczChQ.png" /></figure><p>Compound components are components that work together to form a complete UI.</p><h3><strong>Implicit State</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*8ONq11-l8FnhAiZnfpVG9A.png" /></figure><p>By mapping all children to new child with additional props passed implicitly.</p><h3><strong>Flexible Compound Components</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*RG6Z5oOToDyJD_u4x7m4JQ.png" /></figure><p>We need some way for our compound components to implicitly accept the props regardless of where they’re rendered within the Toggle component’s “posterity”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*MSam1WmKJald30c74P6NQA.png" /></figure><p>The way we do this is through <strong>context</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/295/1*rOaj5Ix502YqYDyi72xqGQ.png" /></figure><p>We can create a context system, and provide the things we want to pass to the children into Provider, and consume the context in child.</p><h3><strong>Prop Collections</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*Hk0XGals0nz7qiBWdzNVTw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*vLQBJOadE9zF_lpxeDA6CA.png" /></figure><p>In more complex components, the list of props that need to be applied to elements can be extensive, so it can be a good idea to take the common use cases for our hook or components and <strong>make objects of props that people can simply spread across the UI they render</strong>.</p><h3><strong>Prop Getters</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*fb1OoVQyudvjXpGf_AY-7Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*4teX-W2cYWzGiDMx3tXOcw.png" /></figure><p>This allow us our consumers like button to have their own logic and whatever else they want to put.</p><p>Getting enhanced capabilities from Props Getters.</p><h3><strong>State Reducer</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*jl21hVhKFBS0NCBMzavEWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*XhTN3XS4iLCdnOFBSlX98w.png" /></figure><p>By using this pattern, it allows our custom hooks or components to specify their own reducer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0fcad56e43b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/advanced-react-patterns-note-a0fcad56e43b">Advanced React Patterns Note</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks Note]]></title>
            <link>https://medium.com/shunze0925/react-hooks-note-ed6f76cba7d0?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed6f76cba7d0</guid>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Thu, 04 Feb 2021 10:02:20 GMT</pubDate>
            <atom:updated>2021-02-05T10:14:44.442Z</atom:updated>
            <content:encoded><![CDATA[<h3>【React】React Hooks</h3><blockquote>The actual contents comes from Epic React’s React Hooks Workshop.</blockquote><blockquote>If you have interests, you can check <a href="https://epicreact.dev/">Epic React</a> by Kent C. Dodds.</blockquote><h3>useState</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*AUm31ttDp6O0mtb6TjvajA.png" /></figure><p><strong>useState</strong> is a function that accepts a single argument which is the <strong>initial state</strong>.</p><p>useState returns a pair of values.</p><p>The <strong>first of the pair is the state value</strong> and the <strong>second is a function we can call to update the state</strong>.</p><h3>What is State?</h3><p>State can be defined as: data that changes over time.</p><h3>Lazy State Initialization</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*cQ3ARFLHWEM9p_G2Sr7Yuw.png" /></figure><p>useState allows us to pass a function instead of the actual value, and then it will only call that function to get the state value <strong>when the component is rendered the first time</strong>.</p><h3>useEffect</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*opIzp97I26mku0VBDR0niw.png" /></figure><p>useEffect is a built-in hook that allows us to run some custom code <strong>after component render and re-render</strong>.</p><p>It accepts a callback function which React will call after <strong>browser painting</strong>.</p><p>But there are various reasons a component can be re-rendered like when a <strong>parent component</strong> in the application tree gets re-rendered.</p><p>So we need a way to prevent side effects run frequently.</p><h3>Dependency Array</h3><p>useEffect allows us to pass a second argument called the “<strong>dependency array</strong>” which signals to React that your effect callback function should be called <strong>when (and only when) those dependencies change</strong>.</p><h3><strong>useCallback</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*zogHTXoRyEVlK29TJl9Ebg.png" /></figure><p>We pass React a function and React gives that same function back to us, but with a catch.</p><p>On subsequent renders, if the elements in the <strong>dependency list are</strong> <strong>unchanged</strong>, React will give us the<strong> same function</strong> it gave us last time.</p><p>So while we still create a new function every render (to pass to useCallback), React only gives us the <strong>new one</strong> if the <strong>dependency list changes</strong>.</p><h3><strong>useMemo</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*9GibBYaV6NSuk7718RBfJw.png" /></figure><p>useMemo is similar to useCallback except it allows you to apply memoization to any value type (not just functions).</p><p>It allows us to put that calculation behind a function <strong>which is only called when the dependencies change</strong>.</p><p>As long as those dependencies does not change, the <strong>result of our function will be the same as the last time the function was called</strong>.</p><h3><strong>memo</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*rHwyjcHWfn4wU4pyDtR2pw.png" /></figure><p>React.memo make our component will not re-render simply because its <strong>parent re-rendered</strong>.</p><p>React.memo also accepts a second argument which is a custom compare function that allows us to compare the props and return <strong>true if rendering the component again is unnecessary</strong> and <strong>false if it is necessary</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed6f76cba7d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/shunze0925/react-hooks-note-ed6f76cba7d0">React Hooks Note</a> was originally published in <a href="https://medium.com/shunze0925">shunze0925</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks Introduction]]></title>
            <link>https://4a490101.medium.com/react-hooks-introduction-b5a0faff862a?source=rss-89369fd5c0a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5a0faff862a</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Shunze]]></dc:creator>
            <pubDate>Thu, 04 Feb 2021 08:26:21 GMT</pubDate>
            <atom:updated>2021-02-06T01:10:03.512Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks</h3><p>The actual content comes from Kent C. Dodds.’s Epic React Course.</p><h3><strong>useState</strong></h3><p><strong>useState</strong> is a function that accepts a single argument which is the <strong>initial state</strong>.</p><p>useState returns a pair of values.</p><p>The <strong>first of the pair is the state value</strong> and the <strong>second is a function we can call to update the state</strong>.</p><h3><strong>What is State?</strong></h3><p>State can be defined as: data that changes over time.</p><h3><strong>Lazy State Initialization</strong></h3><p>useState allows us to pass a function instead of the actual value, and then it will only call that function to get the state value <strong>when the component is rendered the first time</strong>.</p><h3><strong>useEffect</strong></h3><p>useEffect is a built-in hook that allows us to run some custom code <strong>after component render and re-render</strong>.</p><p>It accepts a callback function which React will call after <strong>browser painting</strong>.</p><p>But there are various reasons a component can be re-rendered like when a <strong>parent component</strong> in the application tree gets re-rendered.</p><p>So we need a way to prevent side effects run frequently.</p><h3><strong>Dependency Array</strong></h3><p>useEffect allows us to pass a second argument called the “<strong>dependency array</strong>” which signals to React that your effect callback function should be called <strong>when (and only when) those dependencies change</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5a0faff862a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>