Memoize "withPropsFrom" HOC

This commit is contained in:
schroda
2024-12-21 14:13:31 +01:00
parent 34ad331a71
commit f1f660a546

View File

@@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * 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> = () => T; type PropsSourceCreator<T> = () => T;
@@ -19,14 +19,16 @@ export const withPropsFrom = <
propsSources: { [K in keyof SourceProps]: PropsSourceCreator<SourceProps[K]> }, propsSources: { [K in keyof SourceProps]: PropsSourceCreator<SourceProps[K]> },
sourcePropKeys: SourcePropKeys[], sourcePropKeys: SourcePropKeys[],
) => ) =>
forwardRef<HTMLElement, Omit<ComponentProps, SourcePropKeys>>((props, ref) => { memo(
const sourceProps = propsSources.reduce((acc, propsSource) => ({ ...acc, ...propsSource() }), {}); forwardRef<HTMLElement, Omit<ComponentProps, SourcePropKeys>>((props, ref) => {
const sourceProps = propsSources.reduce((acc, propsSource) => ({ ...acc, ...propsSource() }), {});
const selectedProps = Object.fromEntries( const selectedProps = Object.fromEntries(
Object.entries(sourceProps).filter(([key]) => sourcePropKeys.includes(key as SourcePropKeys)), Object.entries(sourceProps).filter(([key]) => sourcePropKeys.includes(key as SourcePropKeys)),
) as Pick<MergeObjectsArray<SourceProps>, SourcePropKeys>; ) as Pick<MergeObjectsArray<SourceProps>, SourcePropKeys>;
const combinedProps = { ...props, ...selectedProps } as unknown as ComponentProps; const combinedProps = { ...props, ...selectedProps } as unknown as ComponentProps;
return <Component {...combinedProps} ref={ref} />; return <Component {...combinedProps} ref={ref} />;
}); }),
);