<?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 Luke Liu on Medium]]></title>
        <description><![CDATA[Stories by Luke Liu on Medium]]></description>
        <link>https://medium.com/@luke-liu?source=rss-b02a20e91998------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_6fZ4CnAybdzW0CoJOCFLA.jpeg</url>
            <title>Stories by Luke Liu on Medium</title>
            <link>https://medium.com/@luke-liu?source=rss-b02a20e91998------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:36:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luke-liu/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[一定要使用 SWR 的三個理由]]></title>
            <link>https://luke-liu.medium.com/%E4%B8%80%E5%AE%9A%E8%A6%81%E4%BD%BF%E7%94%A8-swr-%E7%9A%84%E4%B8%89%E5%80%8B%E7%90%86%E7%94%B1-42fa925a368f?source=rss-b02a20e91998------2</link>
            <guid isPermaLink="false">https://medium.com/p/42fa925a368f</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[race-condition]]></category>
            <category><![CDATA[swr]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[memory-leak]]></category>
            <dc:creator><![CDATA[Luke Liu]]></dc:creator>
            <pubDate>Sun, 13 Dec 2020 15:32:31 GMT</pubDate>
            <atom:updated>2020-12-15T14:25:29.299Z</atom:updated>
            <content:encoded><![CDATA[<p>Very handy that you’ve never known</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rpKeqsaIn-YduIKtC-wIQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/0*CS3OCTSBtzAYLX5O" /></figure><p>swr 這個 library 從釋出到現在也有一年多的時間了，一開始關注它時，並不覺得有什麼厲害之處，但隨著工作經驗增加，發現許多複雜難解、撈資料的 bug 或是冗餘的程式，都能套用這個 library 輕鬆化解、簡化。</p><p>它包含了許多開發上非常好用的特性，但卻不常被拿來討論，主要是因為大家覺得它似乎很普通，但其實解決了不少 data fetching 使用情境上的痛點，今天想要以幾個要點來分享使用 swr 的理由。</p><h3>理由一：Remote data 存放在 global state 自行維護需要花費較多心力</h3><p>一般而言，開發 react application 一定會用到數個 libraries，redux 是其中之一，這個 state management 工具，有幾種用法，例如將 ajax response 回來的資料存放在 redux store，方便在不同的 component 之間共享使用。雖然 redux 官網標榜著 redux 是 a predictable state container for JS apps，減少許多 bugs 的可能性了，但終究是 global state，還是會產生不預期的行為，困擾開發者們，還是謹慎使用為妙吧，以下舉個例子：</p><p>假設我們有一個 component，需要在每次掛載的時候，fetch 新的資料，通常要這樣做：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*27ikfnQvUxpfHWeV" /></figure><p>但為了避免新掛載的 component 被舊資料干擾，所以 component 卸載前，要手動清除 redux 裡面的舊資料，如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*83-Jf-F_anwUHyiJ" /></figure><p>上面的例子很簡單，但反應出，fetch data 這件事，開發者們是需要依照不同的情境，做各種冗餘的處置，而且情境越複雜，就越難處理，swr 把一些常見的 fetch data 的瑣碎細節處理好了，以下提出幾個自己在實務上，被困擾很久，關於 remote data 很難做好的部分。</p><h3>理由二：Auto refresh and refresh on focus</h3><p>swr 提供了options 讓開發者很簡單的就能使用 auto refresh、refresh on focus</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wdNdlyhK1qAZQxcg" /></figure><p>如果這些功能自行開發的話，要考慮不少問題，例如</p><p>該如何暫停或恢復 auto refresh？</p><p>如何取消已發送的請求？</p><p>component 卸載或掛載何時要對 API setInvterval 或是 clearInterval？</p><p>完成的功能有 memory leaks，但 code 寫太難懂，根本改不動，極難看出哪裡有問題</p><p>需求越複雜越麻煩，後果就是產出一堆爛 code :(</p><h3>理由三：Race conditions</h3><p>*這裡指的 race conditions 並不是作業系統內多執行序的 race condition</p><p>開發應用程式時，除了使用 auto refresh 之外，通常也有能修改資料的 form 表單，完成後再把 form result response 回來，顯示新的資料畫面。此時產生一個難解的問題，如果 form submit 時，auto refresh 提早數毫秒觸發，且 auto refresh 的 response 比 form submit 還要慢，就會造成顯示在畫面的結果可能不是期待的樣子，而是舊資料顯示在畫面上，這便是所謂的 race conditions，如圖所示：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HDeI6E3XxGck1K8Y" /></figure><p>然而 race conditions 並不只以上一個例子，swr 都幫忙處理掉了，在 source code 裡面有相關的註解，解釋對於這類問題的應對方法：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ANWhCnvjsm_MmVZg" /></figure><p>能分成兩種狀況來處理：</p><ol><li>若當前的 request1，比另一個 request2 還要早送出，則忽略 request1</li><li>若有任何的 mutate （指的是修改資料後送出的 request），則忽略其他任何 request 的 response</li></ol><h3>總結</h3><p>使用 redux （or 其他 global state management 😅😅😅）或許是 modern front-end 都需要的。然而謹慎地使用 global state（或是使用 global variable），這其實是學習寫程式的時候，老師都會教的， Functional Programming 也提到 global variable 會產生 side effects，導致難以預期的錯誤，而且，不管是 auto refresh、refresh on focus、memory leaks、race conditions…等等都是挺困擾的部分，若我們把 remote data 交給 swr 幫忙處理，肯定是能使開發上、使用者經驗上更好</p><p><strong><em>希望本篇文章，能帶來一點啟發，如果你覺得我的文章幫助到你，希望你也可以為文章拍手～</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42fa925a368f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>