Reputation: 3357
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
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
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
Reputation: 120
In the documentation for removeEventListener()
only the type and handler are necessary: removeEventListener(type, handler);
Upvotes: 1