Skip to content

Commit

Permalink
feat: simplify custom context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaninvents committed Jun 12, 2019
1 parent 69006eb commit 062409f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/hooks/useReduxContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<Provider>`, if you're not using the default.
* @returns {any} the value of the `ReactReduxContext`
*
* @example
Expand All @@ -19,8 +18,8 @@ import { ReactReduxContext } from '../components/Context'
* return <div>{store.getState()}</div>
* }
*/
export function useReduxContext(context = ReactReduxContext) {
const contextValue = useContext(context)
export function useReduxContext() {
const contextValue = useContext(ReactReduxContext)

invariant(
contextValue,
Expand Down
17 changes: 14 additions & 3 deletions src/hooks/useSelector.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -100,10 +107,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,
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useStore.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
}
Expand Down

0 comments on commit 062409f

Please sign in to comment.