Skip to content

Commit

Permalink
Add JSDocs for useDispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Jan 5, 2024
1 parent 0f65c2a commit 0e60aa7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
47 changes: 43 additions & 4 deletions src/hooks/useDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,45 @@ import type { ReactReduxContextValue } from '../components/Context'
import { ReactReduxContext } from '../components/Context'
import { createStoreHook, useStore as useDefaultStore } from './useStore'

/**
* Represents a custom hook that provides a dispatch function
* from the Redux store.
*
* @template DispatchType - The specific type of the dispatch function.
*
* @since 9.1.0
* @public
*/
export interface UseDispatch<
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>
> {
/**
* Returns the dispatch function from the Redux store.
*
* @returns The dispatch function from the Redux store.
*
* @template AppDispatch - The specific type of the dispatch function.
*/
<AppDispatch extends DispatchType = DispatchType>(): AppDispatch

/**
* Creates a "pre-typed" version of {@linkcode useDispatch useDispatch}
* where the type of the `dispatch` function is predefined.
*
* This allows you to set the `dispatch` type once, eliminating the need to
* specify it with every {@linkcode useDispatch useDispatch} call.
*
* @returns A pre-typed `useDispatch` with the dispatch type already defined.
*
* @example
* ```ts
* const useAppDispatch = useDispatch.withTypes<AppDispatch>()
* ```
*
* @template OverrideDispatchType - The specific type of the dispatch function.
*
* @since 9.1.0
*/
withTypes: <
OverrideDispatchType extends DispatchType
>() => UseDispatch<OverrideDispatchType>
Expand All @@ -22,10 +56,15 @@ export interface UseDispatch<
* @returns {Function} A `useDispatch` hook bound to the specified context.
*/
export function createDispatchHook<
S = unknown,
A extends Action = UnknownAction
StateType = unknown,
ActionType extends Action = UnknownAction
>(
// @ts-ignore
>(context?: Context<ReactReduxContextValue<S, A> | null> = ReactReduxContext) {
context?: Context<ReactReduxContextValue<
StateType,
ActionType
> | null> = ReactReduxContext
) {
const useStore =
context === ReactReduxContext ? useDefaultStore : createStoreHook(context)

Expand All @@ -38,7 +77,7 @@ export function createDispatchHook<
withTypes: () => useDispatch,
})

return useDispatch as UseDispatch<Dispatch<A>>
return useDispatch as UseDispatch<Dispatch<ActionType>>
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/typetests/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ function testShallowEqual() {
)
expectExactType<string>(selected1)

// const useAppSelector: UseSelector<TestState> = useSelector
const useAppSelector = useSelector.withTypes<TestState>()

const selected2 = useAppSelector((state) => state.stateProp, shallowEqual)
Expand Down

0 comments on commit 0e60aa7

Please sign in to comment.