Skip to content

Commit e9631ae

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

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
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: 14 additions & 3 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

@@ -100,10 +107,14 @@ function useSelectorWithStoreAndSubscription(
100107
* @returns {Function} A `useSelector` hook bound to the specified context.
101108
*/
102109
export function createSelectorHook(context = ReactReduxContext) {
110+
const useReduxContext =
111+
context === ReactReduxContext
112+
? useDefaultReduxContext
113+
: () => useContext(context)
103114
return function useSelector(selector, equalityFn = refEquality) {
104115
invariant(selector, `You must pass a selector to useSelectors`)
105116

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

108119
return useSelectorWithStoreAndSubscription(
109120
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)
1116
return function useStore() {
12-
const { store } = useReduxContext(context)
17+
const { store } = useReduxContext()
1318
return store
1419
}
1520
}

0 commit comments

Comments
 (0)