| [a762898] | 1 | import type { Context } from 'react'
|
|---|
| 2 | import type { Action, Store } from 'redux'
|
|---|
| 3 | import type { ReactReduxContextValue } from '../components/Context'
|
|---|
| 4 | import { ReactReduxContext } from '../components/Context'
|
|---|
| 5 | import {
|
|---|
| 6 | createReduxContextHook,
|
|---|
| 7 | useReduxContext as useDefaultReduxContext,
|
|---|
| 8 | } from './useReduxContext'
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Represents a type that extracts the action type from a given Redux store.
|
|---|
| 12 | *
|
|---|
| 13 | * @template StoreType - The specific type of the Redux store.
|
|---|
| 14 | *
|
|---|
| 15 | * @since 9.1.0
|
|---|
| 16 | * @internal
|
|---|
| 17 | */
|
|---|
| 18 | export type ExtractStoreActionType<StoreType extends Store> =
|
|---|
| 19 | StoreType extends Store<any, infer ActionType> ? ActionType : never
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Represents a custom hook that provides access to the Redux store.
|
|---|
| 23 | *
|
|---|
| 24 | * @template StoreType - The specific type of the Redux store that gets returned.
|
|---|
| 25 | *
|
|---|
| 26 | * @since 9.1.0
|
|---|
| 27 | * @public
|
|---|
| 28 | */
|
|---|
| 29 | export interface UseStore<StoreType extends Store> {
|
|---|
| 30 | /**
|
|---|
| 31 | * Returns the Redux store instance.
|
|---|
| 32 | *
|
|---|
| 33 | * @returns The Redux store instance.
|
|---|
| 34 | */
|
|---|
| 35 | (): StoreType
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Returns the Redux store instance with specific state and action types.
|
|---|
| 39 | *
|
|---|
| 40 | * @returns The Redux store with the specified state and action types.
|
|---|
| 41 | *
|
|---|
| 42 | * @template StateType - The specific type of the state used in the store.
|
|---|
| 43 | * @template ActionType - The specific type of the actions used in the store.
|
|---|
| 44 | */
|
|---|
| 45 | <
|
|---|
| 46 | StateType extends ReturnType<StoreType['getState']> = ReturnType<
|
|---|
| 47 | StoreType['getState']
|
|---|
| 48 | >,
|
|---|
| 49 | ActionType extends Action = ExtractStoreActionType<Store>,
|
|---|
| 50 | >(): Store<StateType, ActionType>
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Creates a "pre-typed" version of {@linkcode useStore useStore}
|
|---|
| 54 | * where the type of the Redux `store` is predefined.
|
|---|
| 55 | *
|
|---|
| 56 | * This allows you to set the `store` type once, eliminating the need to
|
|---|
| 57 | * specify it with every {@linkcode useStore useStore} call.
|
|---|
| 58 | *
|
|---|
| 59 | * @returns A pre-typed `useStore` with the store type already defined.
|
|---|
| 60 | *
|
|---|
| 61 | * @example
|
|---|
| 62 | * ```ts
|
|---|
| 63 | * export const useAppStore = useStore.withTypes<AppStore>()
|
|---|
| 64 | * ```
|
|---|
| 65 | *
|
|---|
| 66 | * @template OverrideStoreType - The specific type of the Redux store that gets returned.
|
|---|
| 67 | *
|
|---|
| 68 | * @since 9.1.0
|
|---|
| 69 | */
|
|---|
| 70 | withTypes: <
|
|---|
| 71 | OverrideStoreType extends StoreType,
|
|---|
| 72 | >() => UseStore<OverrideStoreType>
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Hook factory, which creates a `useStore` hook bound to a given context.
|
|---|
| 77 | *
|
|---|
| 78 | * @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
|
|---|
| 79 | * @returns {Function} A `useStore` hook bound to the specified context.
|
|---|
| 80 | */
|
|---|
| 81 | export function createStoreHook<
|
|---|
| 82 | StateType = unknown,
|
|---|
| 83 | ActionType extends Action = Action,
|
|---|
| 84 | >(
|
|---|
| 85 | // @ts-ignore
|
|---|
| 86 | context?: Context<ReactReduxContextValue<
|
|---|
| 87 | StateType,
|
|---|
| 88 | ActionType
|
|---|
| 89 | > | null> = ReactReduxContext,
|
|---|
| 90 | ) {
|
|---|
| 91 | const useReduxContext =
|
|---|
| 92 | context === ReactReduxContext
|
|---|
| 93 | ? useDefaultReduxContext
|
|---|
| 94 | : // @ts-ignore
|
|---|
| 95 | createReduxContextHook(context)
|
|---|
| 96 | const useStore = () => {
|
|---|
| 97 | const { store } = useReduxContext()
|
|---|
| 98 | return store
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | Object.assign(useStore, {
|
|---|
| 102 | withTypes: () => useStore,
|
|---|
| 103 | })
|
|---|
| 104 |
|
|---|
| 105 | return useStore as UseStore<Store<StateType, ActionType>>
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * A hook to access the redux store.
|
|---|
| 110 | *
|
|---|
| 111 | * @returns {any} the redux store
|
|---|
| 112 | *
|
|---|
| 113 | * @example
|
|---|
| 114 | *
|
|---|
| 115 | * import React from 'react'
|
|---|
| 116 | * import { useStore } from 'react-redux'
|
|---|
| 117 | *
|
|---|
| 118 | * export const ExampleComponent = () => {
|
|---|
| 119 | * const store = useStore()
|
|---|
| 120 | * return <div>{store.getState()}</div>
|
|---|
| 121 | * }
|
|---|
| 122 | */
|
|---|
| 123 | export const useStore = /*#__PURE__*/ createStoreHook()
|
|---|