[第06堂] Redux
什麼是Redux?
Redux 是一套除了 React 外在其他框架下也可以使用的資料流管理工具,他定義了資料流的規範,彌補了 React 在元件變多後狀態難以控管的問題,並將資料集中管理,讓程式更容易去維護和測試。
-
action:包含更新資料的方式 action type 及更新資料時所用的值 action payload。
-
dispatcher: 一個溝通用的函式,會接收 action 物件,執行 action 要做的事情,並傳遞資料給reducer。
-
middlewares:對應接收到的 action 執行動作
-
reducer:接收 dispatch 派發的 action,經過對應 action type 更新 state物件。
-
store:資料中心,整合所有 Reducer,每個專案只能有一個 store,並且可以透過 dispatcher 來更新 state。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 使用者操作畫面觸發事件,發送action
- action透過dispatcher傳送至middlewares以及store
- middlewares呼叫對應的API,store向reducer調用對應state
- middlewares將API回傳的結果透過dispatcher傳送給reducer
- reducer更新state的值
- store接收到state的值有變動重新渲染元件
Redux-Saga
解決了redux 非同步的問題,並且程式可讀性較佳。

- 使用者操作畫面觸發事件發送至saga
- saga呼叫對應的API,並等待API的回傳結果
- 將結果透過dispatcher傳送給store
- store向reducer調用對應state
- reducer更新state的值
- store接收到state的值有變動重新渲染元件