overleaf/services/web/frontend/js/shared/hooks/use-deep-compare-memo.ts
Mathias Jakobsen a54b633726 Merge pull request #10111 from overleaf/mj-lezer-file-outline
[web] Parser backed file outline

GitOrigin-RevId: 0825f4887ba4dae24a14dd1880d07b791d0b4cd9
2022-11-04 09:04:33 +00:00

18 lines
509 B
TypeScript

import { DependencyList, useMemo, useRef } from 'react'
import { isEqual } from 'lodash'
function useDeepCompare(dependencies: DependencyList) {
const ref = useRef<DependencyList>([])
if (!isEqual(ref.current, dependencies)) {
ref.current = dependencies
}
return ref.current
}
export default function useDeepCompareMemo<T>(
factory: () => T,
dependencies: DependencyList
) {
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(factory, useDeepCompare(dependencies))
}