@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
26 lines • 990 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useStableObject = useStableObject;
const react_1 = require("react");
const objectComparison_1 = require("../utils/objectComparison");
/**
* Hook that returns a stable reference to an object that only changes
* when the content of the object actually changes (deep comparison).
*
* This is useful for preventing unnecessary re-renders when objects are
* recreated with the same content on every render.
*
* @param obj - The object to stabilize
* @returns A stable reference to the object
*/
function useStableObject(obj) {
const ref = (0, react_1.useRef)(obj);
// If the new object is deeply equal to the previous one, return the previous reference
if ((0, objectComparison_1.deepEqual)(ref.current, obj)) {
return ref.current;
}
// Otherwise, update the ref and return the new object
ref.current = obj;
return obj;
}
//# sourceMappingURL=useStableObject.js.map