Skip to content
Discussion options

You must be logged in to vote

My recommendation is this:

const useExpensiveState = (input) => {
  const [result] = useAtom(useMemo(() => atom((get) => {
    const value = get(myAtom)
    return expensiveOperation(input, value)
  }), [input]))
  return result
}

selectAtom can work too, but you need useCallback anyway, so doesn't help much.

const useExpensiveState = (input) => {
  const [result] = useAtom(selectAtom(myAtom, useCallback((value) => expensiveOperation(input, value), [input])))
  return result
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@oratorio
Comment options

@dai-shi
Comment options

@oratorio
Comment options

Answer selected by oratorio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants