From f1f660a546410d571f0f8a4b94e30f7b47739948 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sat, 21 Dec 2024 14:13:31 +0100 Subject: [PATCH] Memoize "withPropsFrom" HOC --- src/modules/core/hoc/withPropsFrom.tsx | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/modules/core/hoc/withPropsFrom.tsx b/src/modules/core/hoc/withPropsFrom.tsx index 0dcb69a2..e14ed295 100644 --- a/src/modules/core/hoc/withPropsFrom.tsx +++ b/src/modules/core/hoc/withPropsFrom.tsx @@ -6,7 +6,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { ComponentType, forwardRef } from 'react'; +import { ComponentType, forwardRef, memo } from 'react'; type PropsSourceCreator = () => T; @@ -19,14 +19,16 @@ export const withPropsFrom = < propsSources: { [K in keyof SourceProps]: PropsSourceCreator }, sourcePropKeys: SourcePropKeys[], ) => - forwardRef>((props, ref) => { - const sourceProps = propsSources.reduce((acc, propsSource) => ({ ...acc, ...propsSource() }), {}); + memo( + forwardRef>((props, ref) => { + const sourceProps = propsSources.reduce((acc, propsSource) => ({ ...acc, ...propsSource() }), {}); - const selectedProps = Object.fromEntries( - Object.entries(sourceProps).filter(([key]) => sourcePropKeys.includes(key as SourcePropKeys)), - ) as Pick, SourcePropKeys>; + const selectedProps = Object.fromEntries( + Object.entries(sourceProps).filter(([key]) => sourcePropKeys.includes(key as SourcePropKeys)), + ) as Pick, SourcePropKeys>; - const combinedProps = { ...props, ...selectedProps } as unknown as ComponentProps; + const combinedProps = { ...props, ...selectedProps } as unknown as ComponentProps; - return ; - }); + return ; + }), + );