Solved

purchases listener

  • 23 August 2022
  • 1 reply
  • 689 views

Badge +4

[React native IAP implementation]

Hello!  I’m implementing a redeem code button that will redeem discounted annual products in my app. The way i implemented to check it was add a purchasesListener when he clicks the redeem code button, the problem is I want to remove the listener when he  goes from page A ( page with redeem code button ) to another page B ( meaning he did not reclaim the coupon ), or when he enters another specific page C in our app, that is se screen after the paywall if he did not claim any offer codes

I don’t know if I agree with the interface of this purchases listener. Having to pass a function reference when i add and when i remove means i have to keep track of this function reference in some sort of global store or do some workarounds only to keep the exact reference i used to remove…

Couldn’t the sdk accept a function and keep inside the sdk the reference to the function, making it immutable across app’s lifecycle?

I thought it this way

→ add purchasesListener ( callback )

→ Sdk keep it stored inside with it’s reference closed for change ( only overrides maybe )

→ I call remove purchasesListener ( no params )

→ Sdk see if there’s a function ref attached and remove it

I think this would be much cleaner and nicer to whomever is implementing using the sdk

 

Thanks for the answer in advance!

icon

Best answer by Jens 23 August 2022, 16:24

View original

1 reply

Userlevel 5
Badge +7

Hi, the reason that the interface is this way is that you can add / remove multiple different listeners, so the remove method needs to know which one you want to remove. However, that use case should be pretty easy to achieve without any global store, E.g. Something like this (pseudocode):

 

useEffect(() => { 

  const listener = info => { 

    // …

  }
  Purchases.addCustomerInfoUpdateListener(listener);

  return () => Purchases.removeCustomerInfoUpdateListener(listener);

}, []);

 

This hook will add the listener when the component gets mounted and remove it when it gets dismounted. (Note that this uses the newest version of the SDK, the listener used to be called PurchaserInfoUpdateListener).

Reply