<?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 Wanlin Lin on Medium]]></title>
        <description><![CDATA[Stories by Wanlin Lin on Medium]]></description>
        <link>https://medium.com/@leo12345639?source=rss-32a8bdac9fdd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Wanlin Lin on Medium</title>
            <link>https://medium.com/@leo12345639?source=rss-32a8bdac9fdd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 05:32:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leo12345639/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[React 如何 Reconcile?]]></title>
            <link>https://medium.com/@leo12345639/react-%E5%A6%82%E4%BD%95-reconcile-e735e8324ff8?source=rss-32a8bdac9fdd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e735e8324ff8</guid>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Wanlin Lin]]></dc:creator>
            <pubDate>Sat, 16 Jun 2018 03:05:53 GMT</pubDate>
            <atom:updated>2018-09-21T07:04:21.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FL8ABWI0y7IOds8WVh-iMQ.jpeg" /><figcaption>Eswatini Sibebe 史瓦帝尼世界第二大岩石山脈</figcaption></figure><p>前幾天看了 <a href="https://itnext.io/redux-ruins-you-react-app-performance-you-are-doing-something-wrong-82e28ec96cf5">Redux ruins your React app performance? You are doing something wrong</a> 後，發現自己寫 Component 的時候多了很多可以注意的地方，因此再次拜讀了 <a href="https://reactjs.org/docs/reconciliation.html">Reconciliation — React</a> 官方文件，並把它翻成自己看得熟悉的說法。</p><p>以下的段落與官方文件一樣，我猜 React 本身程式碼中比較/更新 DOM 的方式就是依這樣的優先順序（還沒拜讀過 React source code 🙈）</p><h4>若 DOM element type 不一樣</h4><p>若 DOM element type 不一樣，React 就會把這個 node 以下的所有 node unmount and mount。</p><blockquote>Going from &lt;a&gt; to &lt;img&gt;, or from &lt;Article&gt; to &lt;Comment&gt;, or from &lt;Button&gt; to &lt;div&gt; - any of those will lead to a full rebuild.</blockquote><p>像下面這兩個 DOM tree，雖然裡面的&lt;Counter /&gt;可能沒有改變，但因為 &lt;div&gt; 被換成了&lt;span&gt;，所以整個 tree 依然會被刪除並更新。</p><pre>&lt;div&gt;<br>  &lt;Counter /&gt;<br>&lt;/div&gt;<br><br>&lt;span&gt;<br>  &lt;Counter /&gt;<br>&lt;/span&gt;</pre><h4>若 DOM element type 一樣</h4><p>如果 DOM element type 一樣，就會看 DOM element attributes 的變更，有改變就會只更新那些 attributes。</p><pre>&lt;div style={{color: &#39;red&#39;, fontWeight: &#39;bold&#39;}} /&gt;<br><br>&lt;div style={{color: &#39;green&#39;, fontWeight: &#39;bold&#39;}} /&gt;</pre><p>若是使用 style attribute 也不用擔心裡面的 object 是不同的 instance，React 會自己比較並更新不一樣的 style properties。</p><blockquote>When updating style, React also knows to update only the properties that changed.</blockquote><h4>若 Component Elements type 一樣</h4><p>Component 更新了（不確定這邊指的更新的完整意義，應該是包含 state, props, context…等等的更新），把 child component 用新的 props 重新 render() ，並用上述的 DOM element diff 方式判斷如何更新 DOM。</p><p>list of elements 要用 key prop 來幫助 React 做 diff 的部分就不提了。不過一般比較容易忽略的是，其實我們常用的 children prop 通常也是一個 list，因此有機會可以注意是不是有哪些 render 的寫法讓 React 做了不是預想中的更新。這點說得有點簡略，<a href="https://itnext.io/redux-ruins-you-react-app-performance-you-are-doing-something-wrong-82e28ec96cf5">Redux ruins your React app performance? You are doing something wrong</a> 有提到，可以前往閱讀。</p><h4>備註</h4><p>React 使用的演算法比較/更新 DOM 基於下列兩個假設：</p><ol><li>兩種不同的 DOM/Component element type 會 render 出不同的 DOM tree</li><li>開發者會用 key prop 來告知 React 哪些 child elements 在不同的 render 結果中其實是是同一個 element，讓 React 不會因為順序的不同而 remount 這些 elements</li></ol><p>因此如果 render 的寫法寫了不符合他們的假設，就會導致效能低落，例如：</p><ol><li>前後兩次 render 了不同的 element type 但是結果很類似的 component。這會讓 React remount 這些 element，但可能實際上看起來根本沒差。（官方文件說這種案例很少）</li><li>key prop 是不穩定的值，例如是用 random 的方式產生的。這一樣會讓 React remount 這些 element。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e735e8324ff8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>