"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.useMemoizedIdentity = void 0;
var _react = require("react");
var _isEqual = _interopRequireDefault(require("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
 * ```
 */
var 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.
  var valueRef = (0, _react.useRef)(value);
  if (!(0, _isEqual.default)(valueRef.current, value)) {
    valueRef.current = value;
  }
  return valueRef.current;
};
exports.useMemoizedIdentity = useMemoizedIdentity;
//# sourceMappingURL=useMemoizedIdentity.js.map