diff --git a/src/hooks/useReduxContext.js b/src/hooks/useReduxContext.js index 2a526ac2e..903a11c44 100644 --- a/src/hooks/useReduxContext.js +++ b/src/hooks/useReduxContext.js @@ -6,7 +6,6 @@ import { ReactReduxContext } from '../components/Context' * A hook to access the value of the `ReactReduxContext`. This is a low-level * hook that you should usually not need to call directly. * - * @param {Function} [context=ReactReduxContext] Context passed to your ``, if you're not using the default. * @returns {any} the value of the `ReactReduxContext` * * @example @@ -19,8 +18,8 @@ import { ReactReduxContext } from '../components/Context' * return
{store.getState()}
* } */ -export function useReduxContext(context = ReactReduxContext) { - const contextValue = useContext(context) +export function useReduxContext() { + const contextValue = useContext(ReactReduxContext) invariant( contextValue, diff --git a/src/hooks/useSelector.js b/src/hooks/useSelector.js index 9d272ee61..97d72f83b 100644 --- a/src/hooks/useSelector.js +++ b/src/hooks/useSelector.js @@ -1,6 +1,13 @@ -import { useReducer, useRef, useEffect, useMemo, useLayoutEffect } from 'react' +import { + useReducer, + useRef, + useEffect, + useMemo, + useLayoutEffect, + useContext +} from 'react' import invariant from 'invariant' -import { useReduxContext } from './useReduxContext' +import { useReduxContext as useDefaultReduxContext } from './useReduxContext' import Subscription from '../utils/Subscription' import { ReactReduxContext } from '../components/Context' @@ -46,10 +53,14 @@ function useSelectorWithStoreAndSubscription( selectedState = latestSelectedState.current } } catch (err) { - let errorMessage = `An error occured while selecting the store state: ${err.message}.` + let errorMessage = `An error occured while selecting the store state: ${ + err.message + }.` if (latestSubscriptionCallbackError.current) { - errorMessage += `\nThe error may be correlated with this previous error:\n${latestSubscriptionCallbackError.current.stack}\n\nOriginal stack trace:` + errorMessage += `\nThe error may be correlated with this previous error:\n${ + latestSubscriptionCallbackError.current.stack + }\n\nOriginal stack trace:` } throw new Error(errorMessage) @@ -100,10 +111,14 @@ function useSelectorWithStoreAndSubscription( * @returns {Function} A `useSelector` hook bound to the specified context. */ export function createSelectorHook(context = ReactReduxContext) { + const useReduxContext = + context === ReactReduxContext + ? useDefaultReduxContext + : () => useContext(context) return function useSelector(selector, equalityFn = refEquality) { invariant(selector, `You must pass a selector to useSelectors`) - const { store, subscription: contextSub } = useReduxContext(context) + const { store, subscription: contextSub } = useReduxContext() return useSelectorWithStoreAndSubscription( selector, diff --git a/src/hooks/useStore.js b/src/hooks/useStore.js index 376578fdb..82edaed01 100644 --- a/src/hooks/useStore.js +++ b/src/hooks/useStore.js @@ -1,5 +1,6 @@ +import { useContext } from 'react' import { ReactReduxContext } from '../components/Context' -import { useReduxContext } from './useReduxContext' +import { useReduxContext as useDefaultReduxContext } from './useReduxContext' /** * Hook factory, which creates a `useStore` hook bound to a given context. @@ -8,8 +9,12 @@ import { useReduxContext } from './useReduxContext' * @returns {Function} A `useStore` hook bound to the specified context. */ export function createStoreHook(context = ReactReduxContext) { + const useReduxContext = + context === ReactReduxContext + ? useDefaultReduxContext + : () => useContext(context).store return function useStore() { - const { store } = useReduxContext(context) + const { store } = useReduxContext() return store } }