2021-02-04 19:15:23 +01:00
|
|
|
import { useState, useLayoutEffect } from 'react';
|
2020-09-02 21:19:04 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { isEqual } from 'lodash';
|
2020-09-02 21:19:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a value and a function to determine equality, return a
|
|
|
|
* referentially equal value if the equality function returns true.
|
|
|
|
* This hook is helpful in avoiding re-renders and effects running
|
|
|
|
* based on an object or value that always changes references but
|
|
|
|
* infrequently changes it's value. By default, uses isEqual from
|
|
|
|
* lodash. This is typically only useful with objects and arrays.
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {T} value - any value to check equality of
|
2020-09-02 21:19:04 +02:00
|
|
|
* @param {(T, T) => boolean} equalityFn - A function to determine equality
|
|
|
|
* @returns {T}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useEqualityCheck(value, equalityFn = isEqual) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const [computedValue, setComputedValue] = useState(value);
|
2020-09-02 21:19:04 +02:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!equalityFn(value, computedValue)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
setComputedValue(value);
|
2020-09-02 21:19:04 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [value, equalityFn, computedValue]);
|
2020-09-02 21:19:04 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return computedValue;
|
2020-09-02 21:19:04 +02:00
|
|
|
}
|