Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useMemo behaviour across components

In my component, I'm using useMemo to run and cache a somewhat expensive query to the browser runtime. So far, it has cut back dramatically on the time required for subsequent renders.

However, the actual first render is still a problem. I'm rendering thousands of instances of my component at once, and it seems that the expensive query gets repeated unnecessarily. The same query result could be used for a lot of those instances, as I'm using only two or three unique inputs for the query at once. The query could be considered pure, as it consistently returns the same result for the same inputs.

I'm left wishing that the memoized return value was available to other instances of the same component, but the profiling data suggests that it is not.

Is there a clean and sustainable way to ensure that a memoized result is shared across all calls to the same function, regardless of the originating component instance?

like image 318
edgerunner Avatar asked Feb 27 '19 11:02

edgerunner


People also ask

Can I use useMemo for components?

useMemo() is a React Hook that we can use to wrap functions within a component. We can use this to ensure that the values within that function are re-computed only when one of its dependencies change.

Can I use useMemo outside component?

useMemo() function can memoize the value returned by your function and keep it in memory across renders. In most situations, it returns a result that you can't run outside of your React component.

What are differences between React memo () and useMemo ()?

I'll explain those three one by one though at first I will show the basic idea. Simply, React. memo is related to 'component', useMemo is related to 'value', useCallback is related to function. To be precise, useMemo return a value, useCallback return a function.


1 Answers

The state that is maintained by React hooks is specific to component instance where they are called.

In order for useMemo or useCallback to have a common state for multiple component instances, they should occur in nearest common parent and be provided with context API to deeply nested children if needed.

In case it should behave in a different way, general-purpose memoization utility should be used, like Lodash memoize:

const expensiveFn = () => ...;

const memoizedExpensiveFn = _.memoize(expensiveFn);

It also allows to have control over cache storage:

memoizedExpensiveFn.Cache = ES6MapWithExpiration;
like image 92
Estus Flask Avatar answered Sep 28 '22 07:09

Estus Flask