UNPKG

881 BJavaScriptView Raw
1import { useContext } from 'react'
2import { ReactReduxContext } from '../components/Context'
3
4/**
5 * A hook to access the value of the `ReactReduxContext`. This is a low-level
6 * hook that you should usually not need to call directly.
7 *
8 * @returns {any} the value of the `ReactReduxContext`
9 *
10 * @example
11 *
12 * import React from 'react'
13 * import { useReduxContext } from 'react-redux'
14 *
15 * export const CounterComponent = ({ value }) => {
16 * const { store } = useReduxContext()
17 * return <div>{store.getState()}</div>
18 * }
19 */
20export function useReduxContext() {
21 const contextValue = useContext(ReactReduxContext)
22
23 if (process.env.NODE_ENV !== 'production' && !contextValue) {
24 throw new Error(
25 'could not find react-redux context value; please ensure the component is wrapped in a <Provider>'
26 )
27 }
28
29 return contextValue
30}