Skip to content

Commit

Permalink
Verify that selector and equalityF of useSelector are functions (#1706)
Browse files Browse the repository at this point in the history
See #1704
  • Loading branch information
gshilin committed Mar 31, 2021
1 parent f4ea60c commit df36f4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/hooks/useSelector.js
Expand Up @@ -107,8 +107,18 @@ export function createSelectorHook(context = ReactReduxContext) {
? useDefaultReduxContext
: () => useContext(context)
return function useSelector(selector, equalityFn = refEquality) {
if (process.env.NODE_ENV !== 'production' && !selector) {
throw new Error(`You must pass a selector to useSelector`)
if (process.env.NODE_ENV !== 'production') {
if (!selector) {
throw new Error(`You must pass a selector to useSelector`)
}
if (typeof selector !== 'function') {
throw new Error(`You must pass a function as a selector to useSelector`)
}
if (typeof equalityFn !== 'function') {
throw new Error(
`You must pass a function as an equality function to useSelector`
)
}
}
const { store, subscription: contextSub } = useReduxContext()

Expand Down
8 changes: 8 additions & 0 deletions test/hooks/useSelector.spec.js
Expand Up @@ -451,6 +451,14 @@ describe('React', () => {
it('throws if no selector is passed', () => {
expect(() => useSelector()).toThrow()
})

it('throws if selector is not a function', () => {
expect(() => useSelector(1)).toThrow()
})

it('throws if equality function is not a function', () => {
expect(() => useSelector((s) => s.count, 1)).toThrow()
})
})
})

Expand Down

0 comments on commit df36f4e

Please sign in to comment.