@@ -140,57 +140,93 @@ export const reactHooksModule = ({
140140 useStore : rrUseStore ,
141141 } ,
142142 unstable__sideEffectsInRender = false ,
143- } : ReactHooksModuleOptions = { } ) : Module < ReactHooksModule > => ( {
144- name : reactHooksModuleName ,
145- init ( api , { serializeQueryArgs } , context ) {
146- const anyApi = api as any as Api <
147- any ,
148- Record < string , any > ,
149- string ,
150- string ,
151- ReactHooksModule
152- >
153- const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks ( {
154- api,
155- moduleOptions : {
156- batch,
157- hooks,
158- unstable__sideEffectsInRender,
159- } ,
160- serializeQueryArgs,
161- context,
162- } )
163- safeAssign ( anyApi , { usePrefetch } )
164- safeAssign ( context , { batch } )
165-
166- return {
167- injectEndpoint ( endpointName , definition ) {
168- if ( isQueryDefinition ( definition ) ) {
169- const {
170- useQuery,
171- useLazyQuery,
172- useLazyQuerySubscription,
173- useQueryState,
174- useQuerySubscription,
175- } = buildQueryHooks ( endpointName )
176- safeAssign ( anyApi . endpoints [ endpointName ] , {
177- useQuery,
178- useLazyQuery,
179- useLazyQuerySubscription,
180- useQueryState,
181- useQuerySubscription,
182- } )
183- ; ( api as any ) [ `use${ capitalize ( endpointName ) } Query` ] = useQuery
184- ; ( api as any ) [ `useLazy${ capitalize ( endpointName ) } Query` ] =
185- useLazyQuery
186- } else if ( isMutationDefinition ( definition ) ) {
187- const useMutation = buildMutationHook ( endpointName )
188- safeAssign ( anyApi . endpoints [ endpointName ] , {
189- useMutation,
190- } )
191- ; ( api as any ) [ `use${ capitalize ( endpointName ) } Mutation` ] = useMutation
143+ ...rest
144+ } : ReactHooksModuleOptions = { } ) : Module < ReactHooksModule > => {
145+ if ( process . env . NODE_ENV !== 'production' ) {
146+ const hookNames = [ 'useDispatch' , 'useSelector' , 'useStore' ] as const
147+ let warned = false
148+ for ( const hookName of hookNames ) {
149+ // warn for old hook options
150+ if ( Object . keys ( rest ) . length > 0 ) {
151+ if ( ( rest as Partial < typeof hooks > ) [ hookName ] ) {
152+ if ( ! warned ) {
153+ console . warn (
154+ 'As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:' +
155+ '\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`'
156+ )
157+ warned = true
158+ }
192159 }
193- } ,
160+ // migrate
161+ // @ts -ignore
162+ hooks [ hookName ] = rest [ hookName ]
163+ }
164+ // then make sure we have them all
165+ if ( typeof hooks [ hookName ] !== 'function' ) {
166+ throw new Error (
167+ `When using custom hooks for context, all ${
168+ hookNames . length
169+ } hooks need to be provided: ${ hookNames . join (
170+ ', '
171+ ) } .\nHook ${ hookName } was either not provided or not a function.`
172+ )
173+ }
194174 }
195- } ,
196- } )
175+ }
176+
177+ return {
178+ name : reactHooksModuleName ,
179+ init ( api , { serializeQueryArgs } , context ) {
180+ const anyApi = api as any as Api <
181+ any ,
182+ Record < string , any > ,
183+ string ,
184+ string ,
185+ ReactHooksModule
186+ >
187+ const { buildQueryHooks, buildMutationHook, usePrefetch } = buildHooks ( {
188+ api,
189+ moduleOptions : {
190+ batch,
191+ hooks,
192+ unstable__sideEffectsInRender,
193+ } ,
194+ serializeQueryArgs,
195+ context,
196+ } )
197+ safeAssign ( anyApi , { usePrefetch } )
198+ safeAssign ( context , { batch } )
199+
200+ return {
201+ injectEndpoint ( endpointName , definition ) {
202+ if ( isQueryDefinition ( definition ) ) {
203+ const {
204+ useQuery,
205+ useLazyQuery,
206+ useLazyQuerySubscription,
207+ useQueryState,
208+ useQuerySubscription,
209+ } = buildQueryHooks ( endpointName )
210+ safeAssign ( anyApi . endpoints [ endpointName ] , {
211+ useQuery,
212+ useLazyQuery,
213+ useLazyQuerySubscription,
214+ useQueryState,
215+ useQuerySubscription,
216+ } )
217+ ; ( api as any ) [ `use${ capitalize ( endpointName ) } Query` ] = useQuery
218+ ; ( api as any ) [ `useLazy${ capitalize ( endpointName ) } Query` ] =
219+ useLazyQuery
220+ } else if ( isMutationDefinition ( definition ) ) {
221+ const useMutation = buildMutationHook ( endpointName )
222+ safeAssign ( anyApi . endpoints [ endpointName ] , {
223+ useMutation,
224+ } )
225+ ; ( api as any ) [ `use${ capitalize ( endpointName ) } Mutation` ] =
226+ useMutation
227+ }
228+ } ,
229+ }
230+ } ,
231+ }
232+ }
0 commit comments