Skip to content

Commit

Permalink
Make parameter of createDispatchWithMiddlewareHookFactory nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Mar 27, 2024
1 parent dff4cd2 commit 8b5d210
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
22 changes: 9 additions & 13 deletions packages/toolkit/src/dynamicMiddleware/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ interface ReactDynamicMiddlewareInstance<
Dispatch extends ReduxDispatch<UnknownAction> = ReduxDispatch<UnknownAction>,
> extends DynamicMiddlewareInstance<State, Dispatch> {
createDispatchWithMiddlewareHookFactory: (
context?: Context<
ReactReduxContextValue<State, ActionFromDispatch<Dispatch>>
>,
context?: Context<ReactReduxContextValue<
State,
ActionFromDispatch<Dispatch>
> | null>,
) => CreateDispatchWithMiddlewareHook<State, Dispatch>
createDispatchWithMiddlewareHook: CreateDispatchWithMiddlewareHook<
State,
Expand All @@ -71,20 +72,15 @@ export const createDynamicMiddleware = <
const instance = cDM<State, Dispatch>()
const createDispatchWithMiddlewareHookFactory = (
// @ts-ignore
context: Context<
ReactReduxContextValue<State, ActionFromDispatch<Dispatch>>
> = ReactReduxContext,
context: Context<ReactReduxContextValue<
State,
ActionFromDispatch<Dispatch>
> | null> = ReactReduxContext,
) => {
const useDispatch =
// @ts-ignore
context === ReactReduxContext
? useDefaultDispatch
: createDispatchHook(
context as Context<ReactReduxContextValue<
State,
ActionFromDispatch<Dispatch>
> | null>,
)
: createDispatchHook(context)
function createDispatchWithMiddlewareHook<
Middlewares extends Middleware<any, State, Dispatch>[],
>(...middlewares: Middlewares) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const typedInstance = createDynamicMiddleware<number, AppDispatch>()
declare const compatibleMiddleware: Middleware<{}, number, AppDispatch>
declare const incompatibleMiddleware: Middleware<{}, string, AppDispatch>

declare const customContext: Context<ReactReduxContextValue>
declare const customContext: Context<ReactReduxContextValue | null>

declare const addedMiddleware: Middleware<(n: 2) => 2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('createReactDynamicMiddleware', () => {
gDM().prepend(dynamicInstance.middleware).concat(staticMiddleware),
})

const context = React.createContext<ReactReduxContextValue>(null as any)
const context = React.createContext<ReactReduxContextValue | null>(null)

const createDispatchWithMiddlewareHook =
dynamicInstance.createDispatchWithMiddlewareHookFactory(context)
Expand Down

0 comments on commit 8b5d210

Please sign in to comment.