Bob Cheng
Web

Redux

Redux 是一個 state management library (不限於 React)

  • action: 現在發生什麼事,一定有一個屬性 type
  • reducer: 根據 action 的 type 決定對應的 state 要怎麼變化
    • pure function
    • 多個 reducer 最終要組合成一個 root reducer
  • store: 每個應用只有一個,管理 root reducer 和儲存所有 State - 使用 dispatch(action) 來呼叫 root reducer 更新 state

    Redux data flow | Source

Redux Toolkit (RTK)

實作上比較會用的,在 Redux 之上提供更好用的工具 API

  • slice: action 和 reducer 的集合
  • useSelector(): 從 store 中讀取特定 state
  • useDispatch(): 跟 dispatch() 一樣

References