Skip to content

Instantly share code, notes, and snippets.

@bgmort
bgmort / useMemoStable.ts
Last active August 9, 2024 22:21
useMemoStable, a useMemo that you can count on to only run once per set of dependency values
import { useRef } from 'react'
/**
* This could also be called `useDerived`. Works like useMemo,
* except it guarantees that it only executes once per change of dependency values,
* which is what `useMemo` is often expected to do, but doesn't guarantee, and doesn't
* do in strict mode.
*
* Differs from `useEffect` in that the value is computed synchronously during render
*/