Là nơi lưu toàn bộ state của ứng dụng.
Giả sử ta có reducer sau:
const initialState = { count: 0 }
function counterReducer(state = initialState, action) {
switch (action.type) {
case "counter/increment":
return {
...state,
count: state.count + action.payload,
}
case "counter/decrement":
return {
...state,
count: state.count - action.payload,
}
default:
return state
}
}Để tạo ra một store dựa trên reducer trên, ta cần sử dụng hàm configureStore của Redux Toolkit:
import { configureStore } from "@reduxjs/toolkit"
export const store = configureStore({ reducer: counterReducer })Có thể sử dụng phương thức getState để lấy ra state hiện tại của store.
store.getState()