Memoized values - am I doing this correctly?
See original GitHub issueI’m trying to create an optimised selector. The selector is shared amongst several components so it accepts a parameter from outside:
const defaultValue = {};
const useSingleValue = id => useStore(
useCallback(state => state[id] || defaultValue, [id])
);
Is this the correct way to go about it?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Memoized values - am I doing this correctly? #106 - GitHub
I'm trying to create an optimised selector. The selector is shared amongst several components so it accepts a parameter from outside: const ...
Read more >Memoization - LearnHowToProgram.com
memoization ; memo() function will call the function with that specific argument and store that in ; const value ). It will memoize...
Read more >When should you memoize in React - Prateek Surana
Take this sandbox for example, how many times do you think this memoized component will render when you are incrementing the count. But...
Read more >How To Use Memoization To Drastically Increase React ...
Memoization is essentially just caching. Imagine a complex function that is slow to run which takes a as an argument. In order to...
Read more >Understanding JavaScript Memoization In 3 Minutes - codeburst
Memoization becomes demystified when you boil it down to key-value pairs. All we're doing is creating an object, checking for existing values ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Long story short, we discussed and changed our recommendation. The main reason is we plan to use
useMutableSource
in v4, and it recommends stable selectors for optimization. So, we recommend it in v3 too, to prepare for v4.v3 (as well as v2) optimizes with stable selectors too to some extent. It’s trivial if the selectors are lightweight. Unnecessary computation is just about calling the selectors. If a selector is not referentially changed and the state is not changed, we skip calling the selector. It has nothing to do with re-renders. If a selector is very lightweight (ex.
state => state.foo
) skipping it is almost nothing. btw, useCallback is not zero cost, so in total we don’t know which is better. The best is define the selector outside render, so we don’t even need useCallback (if the selector doesn’t depend on other values.)@dai-shi thanks that’s a good idea, I might give the hook version a try next time – we could always use something like a lodash or other just safe get to support a string version of the nested prop pretty easily so that should cover a lot of the cases.
Thanks the pointer to react-tracked I’ll check it out. It’s hard to keep “track” of all of the nice state libraries you’ve got on the go! Also thanks for sharing things like zustand and jotai as well 👍