Skip to content

Commit df36f4e

Browse files
authoredMar 31, 2021
Verify that selector and equalityF of useSelector are functions (#1706)
See #1704
1 parent f4ea60c commit df36f4e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

‎src/hooks/useSelector.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,18 @@ export function createSelectorHook(context = ReactReduxContext) {
107107
? useDefaultReduxContext
108108
: () => useContext(context)
109109
return function useSelector(selector, equalityFn = refEquality) {
110-
if (process.env.NODE_ENV !== 'production' && !selector) {
111-
throw new Error(`You must pass a selector to useSelector`)
110+
if (process.env.NODE_ENV !== 'production') {
111+
if (!selector) {
112+
throw new Error(`You must pass a selector to useSelector`)
113+
}
114+
if (typeof selector !== 'function') {
115+
throw new Error(`You must pass a function as a selector to useSelector`)
116+
}
117+
if (typeof equalityFn !== 'function') {
118+
throw new Error(
119+
`You must pass a function as an equality function to useSelector`
120+
)
121+
}
112122
}
113123
const { store, subscription: contextSub } = useReduxContext()
114124

‎test/hooks/useSelector.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,14 @@ describe('React', () => {
451451
it('throws if no selector is passed', () => {
452452
expect(() => useSelector()).toThrow()
453453
})
454+
455+
it('throws if selector is not a function', () => {
456+
expect(() => useSelector(1)).toThrow()
457+
})
458+
459+
it('throws if equality function is not a function', () => {
460+
expect(() => useSelector((s) => s.count, 1)).toThrow()
461+
})
454462
})
455463
})
456464

0 commit comments

Comments
 (0)
Please sign in to comment.