Skip to content

Commit 93d1d48

Browse files
authored
Merge pull request #3400 from EskiMojo14/require-hooks
2 parents 805379b + bd0036b commit 93d1d48

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

docs/rtk-query/usage/customizing-create-api.mdx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You can create your own versions of `createApi` by either specifying non-default
1919

2020
## Customizing the React-Redux Hooks
2121

22-
If you want the hooks to use different versions of `useSelector` or `useDispatch`, such as if you are using a custom context, you can pass these in at module creation:
22+
If you want the hooks to use different versions of `useSelector`, `useDispatch` and `useStore`, such as if you are using a custom context, you can pass these in at module creation:
2323

2424
```ts
2525
import * as React from 'react'
@@ -33,7 +33,13 @@ import {
3333
const MyContext = React.createContext<ReactReduxContextValue>(null as any)
3434
const customCreateApi = buildCreateApi(
3535
coreModule(),
36-
reactHooksModule({ useDispatch: createDispatchHook(MyContext) })
36+
reactHooksModule({
37+
hooks: {
38+
useDispatch: createDispatchHook(MyContext),
39+
useSelector: createSelectorHook(MyContext),
40+
useStore: createStoreHook(MyContext),
41+
},
42+
})
3743
)
3844
```
3945

@@ -81,7 +87,7 @@ export const myModule = (): Module<CustomModule> => ({
8187

8288
return {
8389
injectEndpoint(endpoint, definition) {
84-
const anyApi = (api as any) as Api<
90+
const anyApi = api as any as Api<
8591
any,
8692
Record<string, any>,
8793
string,

packages/toolkit/src/query/createApi.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,13 @@ export type CreateApi<Modules extends ModuleName> = {
219219
* const MyContext = React.createContext<ReactReduxContextValue>(null as any);
220220
* const customCreateApi = buildCreateApi(
221221
* coreModule(),
222-
* reactHooksModule({ useDispatch: createDispatchHook(MyContext) })
222+
* reactHooksModule({
223+
* hooks: {
224+
* useDispatch: createDispatchHook(MyContext),
225+
* useSelector: createSelectorHook(MyContext),
226+
* useStore: createStoreHook(MyContext)
227+
* }
228+
* })
223229
* );
224230
* ```
225231
*

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
581581
api,
582582
moduleOptions: {
583583
batch,
584-
useDispatch,
585-
useSelector,
586-
useStore,
584+
hooks: { useDispatch, useSelector, useStore },
587585
unstable__sideEffectsInRender,
588586
},
589587
serializeQueryArgs,

packages/toolkit/src/query/react/module.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,26 @@ type RR = typeof import('react-redux')
7171

7272
export interface ReactHooksModuleOptions {
7373
/**
74-
* The version of the `batchedUpdates` function to be used
75-
*/
76-
batch?: RR['batch']
77-
/**
78-
* The version of the `useDispatch` hook to be used
79-
*/
80-
useDispatch?: RR['useDispatch']
81-
/**
82-
* The version of the `useSelector` hook to be used
74+
* The hooks from React Redux to be used
8375
*/
84-
useSelector?: RR['useSelector']
76+
hooks?: {
77+
/**
78+
* The version of the `useDispatch` hook to be used
79+
*/
80+
useDispatch: RR['useDispatch']
81+
/**
82+
* The version of the `useSelector` hook to be used
83+
*/
84+
useSelector: RR['useSelector']
85+
/**
86+
* The version of the `useStore` hook to be used
87+
*/
88+
useStore: RR['useStore']
89+
}
8590
/**
86-
* The version of the `useStore` hook to be used
91+
* The version of the `batchedUpdates` function to be used
8792
*/
88-
useStore?: RR['useStore']
93+
batch?: RR['batch']
8994
/**
9095
* Enables performing asynchronous tasks immediately within a render.
9196
*
@@ -115,17 +120,25 @@ export interface ReactHooksModuleOptions {
115120
* const MyContext = React.createContext<ReactReduxContextValue>(null as any);
116121
* const customCreateApi = buildCreateApi(
117122
* coreModule(),
118-
* reactHooksModule({ useDispatch: createDispatchHook(MyContext) })
123+
* reactHooksModule({
124+
* hooks: {
125+
* useDispatch: createDispatchHook(MyContext),
126+
* useSelector: createSelectorHook(MyContext),
127+
* useStore: createStoreHook(MyContext)
128+
* }
129+
* })
119130
* );
120131
* ```
121132
*
122133
* @returns A module for use with `buildCreateApi`
123134
*/
124135
export const reactHooksModule = ({
125136
batch = rrBatch,
126-
useDispatch = rrUseDispatch,
127-
useSelector = rrUseSelector,
128-
useStore = rrUseStore,
137+
hooks = {
138+
useDispatch: rrUseDispatch,
139+
useSelector: rrUseSelector,
140+
useStore: rrUseStore,
141+
},
129142
unstable__sideEffectsInRender = false,
130143
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => ({
131144
name: reactHooksModuleName,
@@ -141,9 +154,7 @@ export const reactHooksModule = ({
141154
api,
142155
moduleOptions: {
143156
batch,
144-
useDispatch,
145-
useSelector,
146-
useStore,
157+
hooks,
147158
unstable__sideEffectsInRender,
148159
},
149160
serializeQueryArgs,

packages/toolkit/src/query/tsHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function assertCast<T>(v: any): asserts v is T {}
77
export function safeAssign<T extends object>(
88
target: T,
99
...args: Array<Partial<NoInfer<T>>>
10-
) {
11-
Object.assign(target, ...args)
10+
): T {
11+
return Object.assign(target, ...args)
1212
}
1313

1414
/**

0 commit comments

Comments
 (0)