2024-05-10
#ReduxRedux#2 Store, Action, Reudcer 基本寫法
#Redux 的運作說明
Redux 有三個核心: store, action, reducer
- 我們會把狀態存在
store裡 - 想讓狀態改變,我們會發送
action - 當
action發送時,就會觸發reducer函式,執行函式裡的內容,用這樣的方式來更新狀態
#Action
action是我們的 app 唯一能和store互動的方式action是個物件,慣例上會包含一個 type 屬性,若有其他資訊會放在 payload 屬性。- 在
Redux裡,還有個東西叫做action creator,它是一個產生action的函式。
// action creator
const orderCake = () => {
// return 一個 action
return {
type: 'ORDER',
quantity: 1,
};
};
#Reducer
- 在
reudcer裡,我們會制定好依據發送的action,state會如何改變。 reducer是一個函式,它接收舊的state和action作為參數,並回傳新的state。
// 初始的 State 狀態
const initialState = {
cake: 200,
};
// 寫好 reducer
// 這邊使用預設參數,若沒有傳入 prevState,就使用 initialState
const reducer = (prevState = initialState, action) => {
// ...做處理
return newState;
};
#Store
- 我們的
state會存放在store裡;整個應用程式只會有一個store。
// 使用 redux 的 createStore 來創建 store
// 它接收我們寫好的 reducer 作為參數
const store = createStore(reudcer);
- 透過
getState()方法來得到state的值。
console.log('Initial state:', store.getState());
- 透過
dispatch(action)方法來更新state。 它接收action作為參數
// 發送 action 來更新 state
store.dispatch(orderCake());
- 透過
subscribe(listiner)來註冊一個 listener,subscribe()接收一個函式作為參數,它會在state改變時被呼叫。
// 偵測狀態改變
store.subscribe(() => {
console.log('Updated state:', store.getState());
});
- 透過
subscribe(listener)回傳的 function 來撤銷 listener。
參考資料
閱讀推薦: