-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(筆記)SWR 原理 #132
Comments
import React, { useEffect, useLayoutEffect } from 'react'
export const IS_SERVER = !isWindowDefined || 'Deno' in window
// React currently throws a warning when using useLayoutEffect on the server.
// To get around it, we can conditionally useEffect on the server (no-op) and
// useLayoutEffect in the browser.
export const useIsomorphicLayoutEffect = IS_SERVER ? useEffect : useLayoutEffect
useIsomorphicLayoutEffect (變數宣告) // call function
// Logic for updating refs.
useIsomorphicLayoutEffect(() => {
fetcherRef.current = fetcher
configRef.current = config
...
}) |
Update(user 執行動作,觸發資料的變動。例如:user 按下 toggle,背後控制 toggle 狀態的 RenderReact Update DOM(React 收到狀態的變更後,利用新的資料畫出 Virtual DOM。)
DOM diff 之後把修改的部分告訴 Browser,Browser paint screen(user 看到最終新的畫面。)
|
|
如果放入 key 的資料是一個 Object 應該使用 useMemo 去控制 rerender 的機制 const params = useMemo(() => ({ id }), [id]);
const { data: post } = useSWR(
["https://jsonplaceholder.typicode.com/posts/", params],
fetcher
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The name “SWR” is derived from stale-while-revalidate, a HTTP cache invalidation strategy popularized by HTTP RFC 5861.
SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data.
TL;DR
stale-while-revalidate 源自於 HTTP Cache-Control header 的一個屬性
這個階段執行 API 的 fetcher 和驗證,然後把執行後的 Promise 裝到 CONCURRENT_PROMISES 裡面
當 dedupe 設定為 true 時,SWR 會將短時間內相同 key 的 request 合併成一次,避免重複觸發相同 request 。
使用 ES6 的 Map 儲存 api response 資料搭配 get, set
比對當前存在 stateRef 的資料,如果新資料和原本的資料相同就直接 continue,不同就更新,並執行 rerender 讓 component 用新的 stateRef 更新畫面
(原碼現在找不到 dispatch)
SWR 在 revalidate 的地方,處理優先順序避免 block 主要的 rendering
原來 Observer Pattern 才是 SWR 最特別的地方
訂閱特定 key 的所有 component 都會在 key 所對應的資料發生變化時
被通知 rerender
參考
開源專案讀起來 | 聽說可以幫你保管資料再決定要不要更新的 SWR
開源專案讀起來 | 從 SWR 來看看實務上的 Observer Pattern(觀察者模式)
觀察者模式 (Observer Pattern)
了解 SWR 的運作機制,How this async state manager works ?
The text was updated successfully, but these errors were encountered: