From f61db93008279c84e72d74f5998c1ee1f8c51064 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 25 Feb 2022 22:52:26 +0200 Subject: [PATCH] [Refactoring] Teact: Simplify `memo` --- src/lib/teact/teact.ts | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/lib/teact/teact.ts b/src/lib/teact/teact.ts index d17fe68cc..2e61bf2ca 100644 --- a/src/lib/teact/teact.ts +++ b/src/lib/teact/teact.ts @@ -3,7 +3,7 @@ import { fastRaf, fastRafPrimary, onTickEnd, onTickEndPrimary, throttleWithPrimaryRaf, throttleWithRaf, } from '../../util/schedulers'; import { flatten, orderBy } from '../../util/iteratees'; -import arePropsShallowEqual, { getUnequalProps } from '../../util/arePropsShallowEqual'; +import { getUnequalProps } from '../../util/arePropsShallowEqual'; import { handleError } from '../../util/handleError'; import { removeAllDelegatedListeners } from './dom-events'; @@ -676,26 +676,10 @@ export function useRef(initial?: T | null) { }), []); } -export function memo(Component: T, areEqual = arePropsShallowEqual, debugKey?: string) { +export function memo(Component: T, debugKey?: string) { return function TeactMemoWrapper(props: Props) { - // eslint-disable-next-line react-hooks/rules-of-hooks - const propsRef = useRef(props); - const renderedRef = useRef(); - - if (!renderedRef.current || (propsRef.current && !areEqual(propsRef.current, props))) { - if (DEBUG && debugKey) { - // eslint-disable-next-line no-console - console.log( - `[Teact.memo] ${Component.name} (${debugKey}): Update is caused by:`, - getUnequalProps(propsRef.current!, props).join(', '), - ); - } - - propsRef.current = props; - renderedRef.current = createElement(Component, props) as VirtualElementComponent; - } - - return renderedRef.current; + // eslint-disable-next-line react-hooks/exhaustive-deps + return useMemo(() => createElement(Component, props), Object.values(props), debugKey); } as T; }