Xstate: Why useConstant?

Created on 31 Dec 2019  Â·  2Comments  Â·  Source: davidkpiano/xstate

I'm curious why useConstant is used as opposed to just using useMemo?

Looks like they both cache the return value of a function(?)

question

Most helpful comment

https://reactjs.org/docs/hooks-reference.html#usememo

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.

We can't allow forgetting created service - we need it to be the same throughout the whole lifetime of a component.

All 2 comments

cc. @Andarist

https://reactjs.org/docs/hooks-reference.html#usememo

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.

We can't allow forgetting created service - we need it to be the same throughout the whole lifetime of a component.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bradwoods picture bradwoods  Â·  3Comments

suku-h picture suku-h  Â·  3Comments

drmikecrowe picture drmikecrowe  Â·  3Comments

mattiamanzati picture mattiamanzati  Â·  3Comments

pke picture pke  Â·  3Comments