This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
checkPaymentMethodsCanPay executed too many times #7031
Labels
type: bug
The issue/PR concerns a confirmed bug.
Describe the bug
As part of the data store refactor, we refactored the code that checks if a payment method can pay. Previously, this was run using a React callback to minimize the number of times this function executed (I think only twice). After the refactor, we are running this function every time a payment method is added, and it runs within a useEffect, so it runs about 10 times, which is not optimal.
This is because of these lines of code, we are checking payments everytime there is a change to the cart store: https://github.com/woocommerce/woocommerce-blocks/blob/feature/data-store-refactor/assets/js/data/cart/index.ts#L35-L38
This is the old code: https://github.com/woocommerce/woocommerce-blocks/blob/trunk/assets/js/base/context/providers/cart-checkout/payment-methods/use-payment-method-registration.ts#L108
This is the new code: https://github.com/woocommerce/woocommerce-blocks/blob/feature/data-store-refactor/assets/js/data/payment-methods/check-payment-methods.ts#L26
We need to investigate when is the best time to run this code. One suggestion I have is that we could change the updateAvailablePaymentMethods() action to take a single payment method as an argument and only check that payment method, rather than ALL payment methods.
We could also find some way of reliably knowing when all the payment methods have been added to the registry and then run the updateAvailablePaymentMethods() function.
The text was updated successfully, but these errors were encountered: