useMemo does not guarantee value will not be recreated
See original GitHub issueFrom the React Hooks docs:
You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.
I think a ref would be more suitable for holding the machine instance - useMemo
could break this hook in future!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
You can't create a constant using useMemo in React
There is no guarantee that the returned value of useMemo is always the same even when the dependencies didn't change.
Read more >React.useMemo does not work to prevent re-renders
Pass a “create” function and an array of dependencies. useMemo will only recompute the memoized value when one of the dependencies has changed....
Read more >`useMemo` and Stable Values - Kyle Shevlin
It is made clear in the docs that this is not a guarantee, but for all intents and purposes, we can treat it...
Read more >How To Avoid Performance Pitfalls in React with memo ...
This will ensure that the value you pass to the CharacterMap component is always a string even if the user has not yet...
Read more >How to useMemo and useCallback: you can remove most of ...
During the initial render, they are not only useless but even harmful: they make React do some additional work. This means that your...
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 Free
Top 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
A ref is where you should store the service. You will want to follow the lazy instantiation example for
useRef
from https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazilyeg
Released in https://github.com/carloslfu/use-machine/tree/v1.1.0.