Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Remove memorization of payment method content due to stale data
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed May 28, 2021
1 parent 135ec39 commit 5ba2c97
Showing 1 changed file with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import {
useExpressPaymentMethods,
usePaymentMethodInterface,
Expand All @@ -10,8 +11,6 @@ import {
isValidElement,
useCallback,
useRef,
useMemo,
useEffect,
} from '@wordpress/element';
import {
useEditorContext,
Expand All @@ -35,11 +34,6 @@ const ExpressPaymentMethods = () => {
const { paymentMethods } = useExpressPaymentMethods();
const previousActivePaymentMethod = useRef( activePaymentMethod );
const previousPaymentMethodData = useRef( paymentMethodData );
const currentPaymentMethodInterface = useRef( paymentMethodInterface );

useEffect( () => {
currentPaymentMethodInterface.current = paymentMethodInterface;
}, [ paymentMethodInterface ] );

const onExpressPaymentClick = useCallback(
( paymentMethodId ) => () => {
Expand All @@ -63,33 +57,38 @@ const ExpressPaymentMethods = () => {
}
}, [ setActivePaymentMethod, setPaymentStatus ] );

const content = useMemo( () => {
const entries = Object.entries( paymentMethods );

if ( ! entries.length ) {
return <li key="noneRegistered">No registered Payment Methods</li>;
}

return entries.map( ( [ id, paymentMethod ] ) => {
const expressPaymentMethod = isEditor
? paymentMethod.edit
: paymentMethod.content;
return isValidElement( expressPaymentMethod ) ? (
<li key={ id } id={ `express-payment-method-${ id }` }>
{ cloneElement( expressPaymentMethod, {
...currentPaymentMethodInterface.current,
onClick: onExpressPaymentClick( id ),
onClose: onExpressPaymentClose,
} ) }
</li>
) : null;
} );
}, [
paymentMethods,
isEditor,
onExpressPaymentClick,
onExpressPaymentClose,
] );
/**
* @todo Find a way to Memorize Express Payment Method Content
*
* Payment method content could potentially become a bottleneck if lots of logic is ran in the content component. It
* Currently re-renders excessively but is not easy to useMemo because paymentMethodInterface could become stale.
* paymentMethodInterface itself also updates on most renders.
*/
const entries = Object.entries( paymentMethods );
const content =
entries.length > 0 ? (
entries.map( ( [ id, paymentMethod ] ) => {
const expressPaymentMethod = isEditor
? paymentMethod.edit
: paymentMethod.content;
return isValidElement( expressPaymentMethod ) ? (
<li key={ id } id={ `express-payment-method-${ id }` }>
{ cloneElement( expressPaymentMethod, {
...paymentMethodInterface,
onClick: onExpressPaymentClick( id ),
onClose: onExpressPaymentClose,
} ) }
</li>
) : null;
} )
) : (
<li key="noneRegistered">
{ __(
'No registered Payment Methods',
'woo-gutenberg-products-block'
) }
</li>
);

return (
<PaymentMethodErrorBoundary isEditor={ isEditor }>
Expand Down

0 comments on commit 5ba2c97

Please sign in to comment.