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

hnordt picture hnordt  Â·  3Comments

hnordt picture hnordt  Â·  3Comments

ifokeev picture ifokeev  Â·  3Comments

amelon picture amelon  Â·  3Comments

laurentpierson picture laurentpierson  Â·  3Comments