getDefaultEnhancers
傳回包含預設增強器清單的陣列。
預期用途
預設情況下,configureStore
會自動將一些增強器加入 Redux store 設定。
const store = configureStore({
reducer: rootReducer,
})
// Store has enhancers added, because the enhancer list was not customized
如果您想要自訂增強器清單,您可以提供一個增強器函數陣列給 configureStore
const store = configureStore({
reducer: rootReducer,
enhancers: () => new Tuple(offline(offlineConfig)),
})
// store specifically has the offline enhancer applied
然而,當您提供 enhancer
選項時,您有責任定義要新增至儲存體的所有增強器(devtools 除外)。configureStore
除了您列出的增強器之外,不會新增任何額外的增強器,包括 middleware 增強器。
如果您想新增一些自訂增強器,但仍希望新增預設增強器,getDefaultEnhancers
會很有用
import { configureStore } from '@reduxjs/toolkit'
import { offline } from '@redux-offline/redux-offline'
import offlineConfig from '@redux-offline/redux-offline/lib/defaults'
import rootReducer from './reducer'
const store = configureStore({
reducer: rootReducer,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers().concat(offline(offlineConfig)),
})
// Store has all of the default middleware + enhancers added, _plus_ the offline enhancer
包含的預設增強器
產生的陣列將永遠包含根據 configureStore
的 middleware
欄位建立的 applyMiddleware
增強器。
此外,包含 autoBatchEnhancer
,以允許「批次處理」低優先權的動作更新。這是 RTK Query 使用的,並且在使用時應能提升效能。
目前,回傳值為
const enhancers = [applyMiddleware, autoBatchEnhancer]
自訂包含的增強器
getDefaultEnhancers
接受一個選項物件,允許透過兩種方式自訂每個增強器(middleware 增強器除外)
- 每個增強器都可以透過傳遞
false
給其對應的欄位來從結果陣列中排除 - 每個增強器都可以透過傳遞對應欄位的匹配選項物件來自訂其選項
此範例顯示如何自訂 autoBatch 增強器
- TypeScript
- JavaScript
import rootReducer from './reducer'
import { configureStore } from '@reduxjs/toolkit'
const store = configureStore({
reducer: rootReducer,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers({
autoBatch: { type: 'tick' },
}),
})
import rootReducer from './reducer'
import { configureStore } from '@reduxjs/toolkit'
const store = configureStore({
reducer: rootReducer,
enhancers: (getDefaultEnhancers) =>
getDefaultEnhancers({
autoBatch: { type: 'tick' },
}),
})
API 參考
interface AutoBatchOptions {
// see "autoBatchEnhancer" page for options
}
interface GetDefaultEnhancersOptions {
autoBatch?: boolean | AutoBatchOptions
}
function getDefaultEnhancers<M extends Middlewares<any>>(
options: GetDefaultEnhancersOptions = {},
): EnhancerArray<[StoreEnhancer<{ dispatch: ExtractDispatchExtensions<M> }>]>