react-native-make-payment icon indicating copy to clipboard operation
react-native-make-payment copied to clipboard

Discussion: use of `viewTreeObserver.addOnGlobalLayoutListener`

Open descorp opened this issue 3 months ago • 1 comments

Hello Google Pay Team,

I've been looking at the GooglePayButtonComponent.kt file and have a question regarding the use of viewTreeObserver.addOnGlobalLayoutListener { requestLayout() } on line 40.

I have two main concerns with this implementation:

  1. Necessity of the listener: In my own testing with a similar React Native/Android setup, the requestLayout() call seems to work as expected without a global layout listener. The view updates its size correctly once the dimensions are available. Could you please provide some context on why this specific workaround is in place? Is it addressing a known issue that I might be unaware of? For a context, I am setting up default minimal size of the view on React level.

  2. Potential Memory Leak: The addOnGlobalLayoutListener is never removed. This is a potential memory leak, as the listener would hold a reference to the PayButton and its context even after the view has been detached. A best practice is to remove the listener using removeOnGlobalLayoutListener in the onDetachedFromWindow lifecycle method.

Thank you for your time and for maintaining this library.

descorp avatar Sep 26 '25 11:09 descorp

hi @descorp this is interesting. the original issue is/was: https://github.com/facebook/react-native/issues/17968

Do you want to submit a PR to remove viewTreeObserver.addOnGlobalLayoutListener { requestLayout() }?

dmengelt avatar Sep 29 '25 11:09 dmengelt