diff --git a/src/utils/hooks/index.ts b/src/utils/hooks/index.ts index 84f218c..ac42752 100644 --- a/src/utils/hooks/index.ts +++ b/src/utils/hooks/index.ts @@ -2,3 +2,5 @@ export * from './cachedFetch' export * from './functionalValue' export type { FunctionalValue } from './functionalValue' + +export * from './usePartialProps' diff --git a/src/utils/hooks/usePartialProps.ts b/src/utils/hooks/usePartialProps.ts new file mode 100644 index 0000000..edaebaf --- /dev/null +++ b/src/utils/hooks/usePartialProps.ts @@ -0,0 +1,12 @@ +import { useMemo } from "react" + +export const usePartialProps = (prop: Partial | null | undefined, defaultValue: T): T => { + const result: T = useMemo(() => { + if (!prop || typeof prop !== 'object') return defaultValue + return { ...defaultValue, ...prop } + }, [prop, defaultValue]) + + return result +} + +export default usePartialProps