injectMemo
import { injectMemo } from '@zedux/react'
An injector that memoizes a value. This is the injector equivalent of React's useMemo()
hook. Like useMemo()
, injectMemo()
will return the same value on subsequent evaluations unless any items in the dependency array change.
Example
Miscellaneous:
const constantVal = injectMemo(() => getExpensiveVal(), [])
const changesWhenDepsChange = injectMemo(getExpensiveVal, [depA, depB])
const changesEveryTimeButJustWhy = injectMemo(getExpensiveVal)
Signature
- Simplified
- TypeScript
injectMemo = (valueFactory, deps?)
declare const injectMemo: <Value = any>(
valueFactory: () => Value,
deps?: InjectorDeps
) => Value
Required. A function that returns the value to memoize.
This function is called on initial evaluation and again every time any dependencies change on subsequent evaluations.
Optional (though you'll always want to pass it). An array containing absolutely anything.
If any items in this array change on a subsequent evaluation, the previously memoized value will be discarded and the valueFactory
will be called again to produce a new value.
Pass an empty array to prevent the value from ever changing, as long as this atom instance is alive.
The memoized value returned from the valueFactory
.