Skip to content

Commit f1ee17d

Browse files
committed
feat: simplify custom context handling
1 parent 69006eb commit f1ee17d

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/hooks/useReduxContext.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { ReactReduxContext } from '../components/Context'
66
* A hook to access the value of the `ReactReduxContext`. This is a low-level
77
* hook that you should usually not need to call directly.
88
*
9-
* @param {Function} [context=ReactReduxContext] Context passed to your `<Provider>`, if you're not using the default.
109
* @returns {any} the value of the `ReactReduxContext`
1110
*
1211
* @example
@@ -19,8 +18,8 @@ import { ReactReduxContext } from '../components/Context'
1918
* return <div>{store.getState()}</div>
2019
* }
2120
*/
22-
export function useReduxContext(context = ReactReduxContext) {
23-
const contextValue = useContext(context)
21+
export function useReduxContext() {
22+
const contextValue = useContext(ReactReduxContext)
2423

2524
invariant(
2625
contextValue,

src/hooks/useSelector.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { useReducer, useRef, useEffect, useMemo, useLayoutEffect } from 'react'
1+
import {
2+
useReducer,
3+
useRef,
4+
useEffect,
5+
useMemo,
6+
useLayoutEffect,
7+
useContext
8+
} from 'react'
29
import invariant from 'invariant'
3-
import { useReduxContext } from './useReduxContext'
10+
import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
411
import Subscription from '../utils/Subscription'
512
import { ReactReduxContext } from '../components/Context'
613

@@ -46,10 +53,14 @@ function useSelectorWithStoreAndSubscription(
4653
selectedState = latestSelectedState.current
4754
}
4855
} catch (err) {
49-
let errorMessage = `An error occured while selecting the store state: ${err.message}.`
56+
let errorMessage = `An error occured while selecting the store state: ${
57+
err.message
58+
}.`
5059

5160
if (latestSubscriptionCallbackError.current) {
52-
errorMessage += `\nThe error may be correlated with this previous error:\n${latestSubscriptionCallbackError.current.stack}\n\nOriginal stack trace:`
61+
errorMessage += `\nThe error may be correlated with this previous error:\n${
62+
latestSubscriptionCallbackError.current.stack
63+
}\n\nOriginal stack trace:`
5364
}
5465

5566
throw new Error(errorMessage)
@@ -100,10 +111,14 @@ function useSelectorWithStoreAndSubscription(
100111
* @returns {Function} A `useSelector` hook bound to the specified context.
101112
*/
102113
export function createSelectorHook(context = ReactReduxContext) {
114+
const useReduxContext =
115+
context === ReactReduxContext
116+
? useDefaultReduxContext
117+
: () => useContext(context)
103118
return function useSelector(selector, equalityFn = refEquality) {
104119
invariant(selector, `You must pass a selector to useSelectors`)
105120

106-
const { store, subscription: contextSub } = useReduxContext(context)
121+
const { store, subscription: contextSub } = useReduxContext()
107122

108123
return useSelectorWithStoreAndSubscription(
109124
selector,

src/hooks/useStore.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { useContext } from 'react'
12
import { ReactReduxContext } from '../components/Context'
2-
import { useReduxContext } from './useReduxContext'
3+
import { useReduxContext as useDefaultReduxContext } from './useReduxContext'
34

45
/**
56
* Hook factory, which creates a `useStore` hook bound to a given context.
@@ -8,8 +9,12 @@ import { useReduxContext } from './useReduxContext'
89
* @returns {Function} A `useStore` hook bound to the specified context.
910
*/
1011
export function createStoreHook(context = ReactReduxContext) {
12+
const useReduxContext =
13+
context === ReactReduxContext
14+
? useDefaultReduxContext
15+
: () => useContext(context).store
1116
return function useStore() {
12-
const { store } = useReduxContext(context)
17+
const { store } = useReduxContext()
1318
return store
1419
}
1520
}

0 commit comments

Comments
 (0)