|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Line | |
|---|
| 1 | import { React } from '../utils/react'
|
|---|
| 2 | import { ReactReduxContext } from '../components/Context'
|
|---|
| 3 | import type { ReactReduxContextValue } from '../components/Context'
|
|---|
| 4 |
|
|---|
| 5 | /**
|
|---|
| 6 | * Hook factory, which creates a `useReduxContext` hook bound to a given context. This is a low-level
|
|---|
| 7 | * hook that you should usually not need to call directly.
|
|---|
| 8 | *
|
|---|
| 9 | * @param {React.Context} [context=ReactReduxContext] Context passed to your `<Provider>`.
|
|---|
| 10 | * @returns {Function} A `useReduxContext` hook bound to the specified context.
|
|---|
| 11 | */
|
|---|
| 12 | export function createReduxContextHook(context = ReactReduxContext) {
|
|---|
| 13 | return function useReduxContext(): ReactReduxContextValue {
|
|---|
| 14 | const contextValue = React.useContext(context)
|
|---|
| 15 |
|
|---|
| 16 | if (process.env.NODE_ENV !== 'production' && !contextValue) {
|
|---|
| 17 | throw new Error(
|
|---|
| 18 | 'could not find react-redux context value; please ensure the component is wrapped in a <Provider>',
|
|---|
| 19 | )
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | return contextValue!
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * A hook to access the value of the `ReactReduxContext`. This is a low-level
|
|---|
| 28 | * hook that you should usually not need to call directly.
|
|---|
| 29 | *
|
|---|
| 30 | * @returns {any} the value of the `ReactReduxContext`
|
|---|
| 31 | *
|
|---|
| 32 | * @example
|
|---|
| 33 | *
|
|---|
| 34 | * import React from 'react'
|
|---|
| 35 | * import { useReduxContext } from 'react-redux'
|
|---|
| 36 | *
|
|---|
| 37 | * export const CounterComponent = () => {
|
|---|
| 38 | * const { store } = useReduxContext()
|
|---|
| 39 | * return <div>{store.getState()}</div>
|
|---|
| 40 | * }
|
|---|
| 41 | */
|
|---|
| 42 | export const useReduxContext = /*#__PURE__*/ createReduxContextHook()
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.