nexora
Version:
A lightweight, production-ready JavaScript library for building user interfaces, supporting JSX.
21 lines (20 loc) • 605 B
JavaScript
import { shallowEqual } from '../../core';
/**
* Freeze a component to prevent unnecessary re-renders
* @param Component - The component to freeze
* @returns A new component that only updates when props change
*/
export function freeze(Component) {
let prevProps = null;
return (props) => {
if (!props && !prevProps) {
return Component(props);
}
const shouldUpdate = !prevProps || !shallowEqual(prevProps, props);
if (shouldUpdate) {
prevProps = { ...props };
return Component(props);
}
return null;
};
}