-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Potentially misleading advice in "Balanced Selector Usage" #4783
Comments
I'm inclined to agree that it's a little less black and white than that snippet makes it out to be. Generally I go by:
|
@EskiMojo14 I think that's reasonable! I lean a bit more conservatively and generally think that anything that's operating over an array should be memoized since you end up with a |
That's actually exactly what this section of the doc is trying to warn against. It's much like the "Should I wrap every component in The nuances of the snippet here are less important than getting across the concept that "sometimes a simple plain unmemoized function is all you actually need here". |
That's fair, but I'd still argue @EskiMojo14's framing is an improvement here - telling people not to memoize expensive computations just because they return a scalar will often be detrimental advice. I can buy that "SHOULD memoize" in my OP may be too strong though. |
Yeah, I think those are reasonable guidelines to have in here. (I still don't think "a bunch of additions" actually qualifies as "expensive" per se, but I get your overall point.) |
Thanks both, put up a small PR to tweak the wording here: #4784. I'm also not sure how many additions would have to be done to be actually expensive, probably quite a lot 😆 |
What docs page needs to be fixed?
Link
What is the problem?
Quoting from the docs:
What should be changed to fix the problem?
I'd be curious here if opinions differ, but I believe the advice above is misleading. Specifically, I think there are plenty of good times you want
reselect
-like memoization even if your selector returns a scalar value.One big benefit of memoization is that the actual selector computation itself doesn't have to rerun in every component using the selector on every redux action if the inputs haven't changed.
Let's say for example you have 100
<Item />
components currently rendered on screen and 1000items
in the redux store. Each<Item />
does something likeuseSelector(selectItemsTotal)
. Without memoization, on every dispatched Redux action you'll end up with 100,000 additions taking place. With memoization, you'll have 1000 additions and 100 equality comparision for memoized selectors which will then opt-out of computation.As a strawman, I'd potentially change the docs to something like the following:
The text was updated successfully, but these errors were encountered: