UNPKG

wix-style-react

Version:
23 lines 887 B
import { useRef } from 'react'; import isEqual from 'lodash/isEqual'; /** * Returns a identity-memoized value. The memoization is based on deep equality. * * @example * ``` * const value = useMemoizedIdentity({ foo: 'bar', baz: { qux: 'quux' } }); * value === useMemoizedIdentity({ foo: 'bar', baz: { qux: 'quux' } }); // true * ``` */ export const useMemoizedIdentity = (value) => { // We're technically breaking the rules of react here. Refs are only allowed // to be initialize once on render and are only allowed to be mutated outside // of render function. But since we're only preserving the identity of a value // if it hasn't changed this should be fine. const valueRef = useRef(value); if (!isEqual(valueRef.current, value)) { valueRef.current = value; } return valueRef.current; }; //# sourceMappingURL=useMemoizedIdentity.js.map