From df36f4efa47a32954b9d14c2c3b5c3e9c0cc795d Mon Sep 17 00:00:00 2001 From: Gregory Shilin Date: Wed, 31 Mar 2021 05:08:06 +0300 Subject: [PATCH] Verify that selector and equalityF of useSelector are functions (#1706) See https://github.com/reduxjs/react-redux/issues/1704 --- src/hooks/useSelector.js | 14 ++++++++++++-- test/hooks/useSelector.spec.js | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/hooks/useSelector.js b/src/hooks/useSelector.js index 87b61f244..85e85d13e 100644 --- a/src/hooks/useSelector.js +++ b/src/hooks/useSelector.js @@ -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() diff --git a/test/hooks/useSelector.spec.js b/test/hooks/useSelector.spec.js index 0af0176c7..17d7a80aa 100644 --- a/test/hooks/useSelector.spec.js +++ b/test/hooks/useSelector.spec.js @@ -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() + }) }) })