Skip to content

Commit b394b04

Browse files
author
ben.durrant
committed
Move hooks under single key, for better DX (breaking change)
1 parent 077da03 commit b394b04

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ export type CreateApi<Modules extends ModuleName> = {
220220
* const customCreateApi = buildCreateApi(
221221
* coreModule(),
222222
* reactHooksModule({
223-
* useDispatch: createDispatchHook(MyContext),
224-
* useSelector: createSelectorHook(MyContext),
225-
* useStore: createStoreHook(MyContext)
223+
* hooks: {
224+
* useDispatch: createDispatchHook(MyContext),
225+
* useSelector: createSelectorHook(MyContext),
226+
* useStore: createStoreHook(MyContext)
227+
* }
226228
* })
227229
* );
228230
* ```

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type {
99
} from '@reduxjs/toolkit/dist/query/endpointDefinitions'
1010
import type { Api, Module } from '../apiTypes'
1111
import { capitalize } from '../utils'
12-
import type { AllOrNone } from '../tsHelpers'
1312
import { safeAssign } from '../tsHelpers'
1413
import type { BaseQueryFn } from '@reduxjs/toolkit/dist/query/baseQueryTypes'
1514

@@ -70,22 +69,24 @@ declare module '@reduxjs/toolkit/dist/query/apiTypes' {
7069

7170
type RR = typeof import('react-redux')
7271

73-
type ReactHooks = {
72+
export interface ReactHooksModuleOptions {
7473
/**
75-
* The version of the `useDispatch` hook to be used
74+
* The hooks from React Redux to be used
7675
*/
77-
useDispatch: RR['useDispatch']
78-
/**
79-
* The version of the `useSelector` hook to be used
80-
*/
81-
useSelector: RR['useSelector']
82-
/**
83-
* The version of the `useStore` hook to be used
84-
*/
85-
useStore: RR['useStore']
86-
}
87-
88-
export type ReactHooksModuleOptions = AllOrNone<ReactHooks> & {
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+
}
8990
/**
9091
* The version of the `batchedUpdates` function to be used
9192
*/
@@ -120,9 +121,11 @@ export type ReactHooksModuleOptions = AllOrNone<ReactHooks> & {
120121
* const customCreateApi = buildCreateApi(
121122
* coreModule(),
122123
* reactHooksModule({
123-
* useDispatch: createDispatchHook(MyContext),
124-
* useSelector: createSelectorHook(MyContext),
125-
* useStore: createStoreHook(MyContext)
124+
* hooks: {
125+
* useDispatch: createDispatchHook(MyContext),
126+
* useSelector: createSelectorHook(MyContext),
127+
* useStore: createStoreHook(MyContext)
128+
* }
126129
* })
127130
* );
128131
* ```
@@ -131,9 +134,11 @@ export type ReactHooksModuleOptions = AllOrNone<ReactHooks> & {
131134
*/
132135
export const reactHooksModule = ({
133136
batch = rrBatch,
134-
useDispatch = rrUseDispatch,
135-
useSelector = rrUseSelector,
136-
useStore = rrUseStore,
137+
hooks = {
138+
useDispatch: rrUseDispatch,
139+
useSelector: rrUseSelector,
140+
useStore: rrUseStore,
141+
},
137142
unstable__sideEffectsInRender = false,
138143
}: ReactHooksModuleOptions = {}): Module<ReactHooksModule> => ({
139144
name: reactHooksModuleName,
@@ -149,9 +154,7 @@ export const reactHooksModule = ({
149154
api,
150155
moduleOptions: {
151156
batch,
152-
useDispatch,
153-
useSelector,
154-
useStore,
157+
hooks,
155158
unstable__sideEffectsInRender,
156159
},
157160
serializeQueryArgs,

packages/toolkit/src/query/tsHelpers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export type Id<T> = { [K in keyof T]: T[K] } & {}
22
export type WithRequiredProp<T, K extends keyof T> = Omit<T, K> &
33
Required<Pick<T, K>>
44
export type Override<T1, T2> = T2 extends any ? Omit<T1, keyof T2> & T2 : never
5-
export type AllOrNone<T> = T | { [K in keyof T]?: never }
65
export function assertCast<T>(v: any): asserts v is T {}
76

87
export function safeAssign<T extends object>(

0 commit comments

Comments
 (0)