braze-inc / braze-react-native-sdk

Public repo for the Braze React Native SDK
https://www.braze.com
Other
64 stars 84 forks source link

[Feature]: Braze.removeEventListener(Braze.Events.PUSH_NOTIFICATION_EVENT); #190

Closed Kailash23 closed 1 year ago

Kailash23 commented 1 year ago

What problem are you facing?

I am not able to remove the added event listener I need to remove the event listener when component unmounts

  useEffect(() => {
    Braze.addListener(Braze.Events.PUSH_NOTIFICATION_EVENT, handleDeeplinkUrl);

    return () => { 
     // Error : Property 'removeEventListener' does not exist on type 'typeof import("/Users/***/node_modules/react-native-appboy-sdk/src/index")'.
      Braze.removeEventListener(Braze.Events.PUSH_NOTIFICATION_EVENT); 
    }
  }, [handleDeeplinkUrl]);

Workarounds

No workarounds

Ideal Solution

No response

Other Information

No response

wesleyorbin commented 1 year ago

Hi @Kailash23. The addListener() function returns an EmitterSubscription object, which has a remove() method that you can call when your component unmounts.

useEffect(() => {
  const listener = Braze.addListener(Braze.Events.PUSH_NOTIFICATION_EVENT, handleDeeplinkUrl);

  return () => { 
    listener.remove();
  }
}, [handleDeeplinkUrl]);
wesleyorbin commented 1 year ago

@Kailash23 It currently only works on Android. You can follow #174 for updates.