react-nativerefactoringdeep-linkingdeprecateddynamic-linking

removeEventListener is Deprecated and i don't achieve to refacto it properly


Linking.removeEventListener('url', onReceiveURL);

removeEventListener is deprecated.

This is what my IDE suggests :

EventEmitter.removeListener('url', ...): Method has been deprecated. Please instead use remove() on the subscription returned by EventEmitter.addListener.

  // Custom function to subscribe to incoming links
  subscribe(listener: (deeplink: string) => void) {
    // First, you may want to do the default deep link handling
    const onReceiveURL = ({url}: {url: string}) => listener(url);
    // Listen to incoming links from deep linking
    Linking.addEventListener('url', onReceiveURL);
    const handleDynamicLink = (
      dynamicLink: FirebaseDynamicLinksTypes.DynamicLink,
    ) => {
      listener(dynamicLink.url);
    };
    const unsubscribeToDynamicLinks = dynamicLinks().onLink(handleDynamicLink);
    return () => {
      unsubscribeToDynamicLinks();
      Linking.removeEventListener('url', onReceiveURL);
    };

I tried many things but nothing seems to work.

Didn't find any concrete information about it.

Any help to figure it out ?

EDIT -> I will investigate further but so far it's working :

 const unsubscribeToDynamicLinks : any = ...

then in return : 
return () => {
unsubscribeToDynamicLinks().remove('url', onReceiveURL);};

 

Solution

  • For the new API, this is how it works:

    
    useEffect (() => {
      const subscription = Linking.addEventListener('url', onReceiveURL);
      return () => subscription.remove();
    }, [])