Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy react hook module concept #4128

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
allow providing options when calling buildHooks
  • Loading branch information
EskiMojo14 committed Jan 27, 2024
commit 0352f3484fa1f3943487e09e6b3de3896be6b94e
56 changes: 33 additions & 23 deletions packages/toolkit/src/query/react/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ declare module '@reduxjs/toolkit/query' {
TagTypes extends string,
> {
[lazyReactHooksModuleName]: {
buildHooks(): ApiModules<
buildHooks(
options?: ReactHooksModuleOptions,
): ApiModules<
BaseQuery,
Definitions,
ReducerPath,
Expand Down Expand Up @@ -170,6 +172,17 @@ function buildInjectEndpoint(
}
}

const defaultOptions: Required<ReactHooksModuleOptions> = {
batch: rrBatch,
hooks: {
useDispatch: rrUseDispatch,
useSelector: rrUseSelector,
useStore: rrUseStore,
},
createSelector: _createSelector,
unstable__sideEffectsInRender: false,
}

/**
* Creates a module that generates react hooks from endpoints, for use with `buildCreateApi`.
*
Expand All @@ -190,17 +203,16 @@ function buildInjectEndpoint(
*
* @returns A module for use with `buildCreateApi`
*/
export const reactHooksModule = ({
batch = rrBatch,
hooks = {
useDispatch: rrUseDispatch,
useSelector: rrUseSelector,
useStore: rrUseStore,
},
createSelector = _createSelector,
unstable__sideEffectsInRender = false,
...rest
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => {
export const reactHooksModule = (
moduleOptions?: ReactHooksModuleOptions,
): Module<ReactHooksModule> => {
const {
batch,
hooks,
createSelector,
unstable__sideEffectsInRender,
...rest
} = { ...defaultOptions, ...moduleOptions }
if (process.env.NODE_ENV !== 'production') {
const hookNames = ['useDispatch', 'useSelector', 'useStore'] as const
let warned = false
Expand Down Expand Up @@ -268,16 +280,9 @@ export const reactHooksModule = ({
}
}

export const lazyReactHooksModule = ({
batch = rrBatch,
hooks = {
useDispatch: rrUseDispatch,
useSelector: rrUseSelector,
useStore: rrUseStore,
},
createSelector = _createSelector,
unstable__sideEffectsInRender = false,
}: ReactHooksModuleOptions = {}): Module<LazyReactHooksModule> => ({
export const lazyReactHooksModule = (
moduleOptions?: ReactHooksModuleOptions,
): Module<LazyReactHooksModule> => ({
name: lazyReactHooksModuleName,
init(api, { serializeQueryArgs }, context) {
const anyApi = api as any as Api<
Expand All @@ -288,7 +293,12 @@ export const lazyReactHooksModule = ({
LazyReactHooksModule
>

function buildEndpointHooks() {
function buildEndpointHooks(options?: ReactHooksModuleOptions) {
const { batch, hooks, unstable__sideEffectsInRender, createSelector } = {
...defaultOptions,
...moduleOptions,
...options,
}
const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks({
api,
moduleOptions: {
Expand Down
Loading