自定义 createApi
目前,RTK Query 包含两个 createApi
变体
createBaseApi
,它只包含与 UI 无关的 Redux 逻辑(核心模块)createApi
,它包含核心模块和 React Hooks 模块
您可以通过为模块指定非默认选项或添加自己的模块来创建自己的 createApi
版本。
自定义 React-Redux Hooks
如果您希望钩子使用不同版本的useSelector
、useDispatch
和 useStore
(例如,如果您使用自定义上下文),您可以在模块创建时传入这些钩子。
- TypeScript
- JavaScript
import * as React from 'react'
import {
createDispatchHook,
createSelectorHook,
createStoreHook,
ReactReduxContextValue,
} from 'react-redux'
import {
buildCreateApi,
coreModule,
reactHooksModule,
} from '@reduxjs/toolkit/query/react'
const MyContext = React.createContext<ReactReduxContextValue | null>(null)
const customCreateApi = buildCreateApi(
coreModule(),
reactHooksModule({
hooks: {
useDispatch: createDispatchHook(MyContext),
useSelector: createSelectorHook(MyContext),
useStore: createStoreHook(MyContext),
},
})
)
import * as React from 'react'
import {
createDispatchHook,
createSelectorHook,
createStoreHook,
} from 'react-redux'
import {
buildCreateApi,
coreModule,
reactHooksModule,
} from '@reduxjs/toolkit/query/react'
const MyContext = React.createContext(null)
const customCreateApi = buildCreateApi(
coreModule(),
reactHooksModule({
hooks: {
useDispatch: createDispatchHook(MyContext),
useSelector: createSelectorHook(MyContext),
useStore: createStoreHook(MyContext),
},
})
)
为 RTKQ 自定义 createSelector
coreModule
和 reactHooksModule
都接受一个 createSelector
选项,该选项应该是一个来自 Reselect 的选择器创建器实例,或者具有等效签名。
- TypeScript
- JavaScript
import * as React from 'react'
import { createSelectorCreator, lruMemoize } from '@reduxjs/toolkit'
import {
buildCreateApi,
coreModule,
reactHooksModule,
} from '@reduxjs/toolkit/query/react'
const createLruSelector = createSelectorCreator(lruMemoize)
const customCreateApi = buildCreateApi(
coreModule({ createSelector: createLruSelector }),
reactHooksModule({ createSelector: createLruSelector })
)
import { createSelectorCreator, lruMemoize } from '@reduxjs/toolkit'
import {
buildCreateApi,
coreModule,
reactHooksModule,
} from '@reduxjs/toolkit/query/react'
const createLruSelector = createSelectorCreator(lruMemoize)
const customCreateApi = buildCreateApi(
coreModule({ createSelector: createLruSelector }),
reactHooksModule({ createSelector: createLruSelector })
)
创建您自己的模块
如果您想创建自己的模块,您应该查看 react-hooks 模块 以了解实现方式。
这是一个非常简化的版本
import { CoreModule } from '@internal/core/module'
import {
BaseQueryFn,
EndpointDefinitions,
Api,
Module,
buildCreateApi,
coreModule,
} from '@reduxjs/toolkit/query'
export const customModuleName = Symbol()
export type CustomModule = typeof customModuleName
declare module '../apiTypes' {
export interface ApiModules<
BaseQuery extends BaseQueryFn,
Definitions extends EndpointDefinitions,
ReducerPath extends string,
TagTypes extends string,
> {
[customModuleName]: {
endpoints: {
[K in keyof Definitions]: {
myEndpointProperty: string
}
}
}
}
}
export const myModule = (): Module<CustomModule> => ({
name: customModuleName,
init(api, options, context) {
// initialize stuff here if you need to
return {
injectEndpoint(endpoint, definition) {
const anyApi = api as any as Api<
any,
Record<string, any>,
string,
string,
CustomModule | CoreModule
>
anyApi.endpoints[endpoint].myEndpointProperty = 'test'
},
}
},
})
export const myCreateApi = buildCreateApi(coreModule(), myModule())