-
I am trying to establish a pattern in my project for memoized state selection. Specifically I want to be able to memoize the output of state operations as shown below for the sake of performance, like what if offered by reselect for Redux.
I looked through the options, and it seems I considered leveraging
It would be great to know if there's a recommended pattern for this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
My recommendation is this: const useExpensiveState = (input) => {
const [result] = useAtom(useMemo(() => atom((get) => {
const value = get(myAtom)
return expensiveOperation(input, value)
}), [input]))
return result
}
const useExpensiveState = (input) => {
const [result] = useAtom(selectAtom(myAtom, useCallback((value) => expensiveOperation(input, value), [input])))
return result
} |
Beta Was this translation helpful? Give feedback.
My recommendation is this:
selectAtom
can work too, but you needuseCallback
anyway, so doesn't help much.