From a700d22b4d3425e251aedbe19c9670706a558026 Mon Sep 17 00:00:00 2001 From: goodmice Date: Sat, 25 Jun 2022 15:45:46 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D1=85=D1=83=D0=BA=20usePartialProps=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20=D0=BE?= =?UTF-8?q?=D0=BF=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=BF=D1=80=D0=BE=D0=BF=D1=81=D0=B0=D0=BC=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0=D0=BC=D0=B8=20?= =?UTF-8?q?=D1=81=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=8C=D1=8E=20=D0=BF=D1=80=D0=B8=D1=81=D0=B2=D0=BE=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=BF=D0=BE-=D1=83=D0=BC=D0=BE=D0=BB=D1=87=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8E=20=D0=B2=20=D0=B3=D0=BB=D1=83=D0=B1=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/hooks/index.ts | 2 ++ src/utils/hooks/usePartialProps.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/utils/hooks/usePartialProps.ts 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