UNPKG

@winglet/react-utils

Version:

React utility library providing custom hooks, higher-order components (HOCs), and utility functions to enhance React application development with improved reusability and functionality

30 lines (27 loc) 955 B
import { useRef, useMemo } from 'react'; const useRestProperties = (props) => { const propsRef = useRef(props); const keysRef = useRef(props ? Object.keys(props) : []); return useMemo(() => { if (!props || props === propsRef.current) return props; const currentKeys = Object.keys(props); if (currentKeys.length !== keysRef.current.length) { propsRef.current = props; keysRef.current = currentKeys; return props; } if (!currentKeys.length) return propsRef.current; for (let i = 0; i < currentKeys.length; i++) { const key = currentKeys[i]; if (props[key] !== propsRef.current[key]) { propsRef.current = props; keysRef.current = currentKeys; return props; } } return propsRef.current; }, [props]); }; export { useRestProperties };