question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memoized values - am I doing this correctly?

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
dai-shicommented, Sep 9, 2020

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

1reaction
alexanderchancommented, Sep 29, 2020

@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 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found