Ben
Ben

Reputation: 3357

Linking.removeEventListener required params

If I declared in a useEffect hook

Linking.addEventListener('url', ({ url }) => {
  handleUrl({ url, userDetails });
});

In the cleanup function of the hook is it enough to write Linking.removeEventListener('url', handleUrl); or do I have to pass the same params as in the declared Linking.addEventListener?

Upvotes: 3

Views: 5691

Answers (3)

amazing
amazing

Reputation: 191

Answer for React Native in 2022, you should remove an event like this:

useEffect(() => {
    const urlListener = Linking.addEventListener('url', handleUrl);

    return () => {
      urlListener.remove();
    };
  }, []);

Upvotes: 8

Sameer Kumar Jain
Sameer Kumar Jain

Reputation: 2135

Always declare the handler first, do not use inline function

const myhandler =({url}) => {
  handleUrl({ url, userDetails });
});

Then attach it to listener

Linking.addEventListener('url', myhandler);

And remove it like

Linking.removeEventListener('url', myhandler);

Upvotes: 7

thomasciv
thomasciv

Reputation: 120

In the documentation for removeEventListener() only the type and handler are necessary: removeEventListener(type, handler);

Upvotes: 1

Related Questions