Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that selector and equalityF of useSelector are functions #1706

Merged
merged 1 commit into from Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to lie, I would love to add a little sass on this one:

Suggested change
`You must pass a function as an equality function to useSelector`
`You must pass a _function_ as an equality _function_ to useSelector...`

(Note, please don't merge this :P)

)
}
}
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