Skip to content

Proposal: check that useSelector() returning a new object uses an equalityFn #91

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

Open
amcsi opened this issue Jun 15, 2022 · 1 comment

Comments

@amcsi
Copy link

amcsi commented Jun 15, 2022

As per the docs here, if you pass a selector callback that returns a new object to useSelector() (e.g. useSelector(state => ({prop: state.someReducer.prop})), then the component will re-render on any state change in the entire store. Thus the docs suggest that you pass an equalityFn as the 2nd parameter to useSelector(); typically shallowEqual that is shipped with react-redux.

It would be cool if the eslint plugin provided a way to enforce this. My colleagues wrote a bunch of useSelector() calls with selectors returning new callbacks, without passing a 2nd argument (shallowEqual).

@ekilah
Copy link

ekilah commented Apr 10, 2024

I ran into this also, and came looking for a lint rule to help me keep our codebase compliant with the error we get (posting for searchability):

Selector returned a different result when called with the same parameters. This can lead to unnecessary rerenders. Selectors that return a new reference (such as an object or an array) should be memoized

However, to satisfy the problem the docs above talk about, I'd instead (or also) want the lint rule to suggest returning a single state value instead of many, like the docs talk about here and here. In other words, to ask them to repeat N useSelector calls instead of using one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants