Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Right way to clean up listeners #1803

Open
yishay-at-bay opened this issue Jun 24, 2024 · 1 comment
Open

Right way to clean up listeners #1803

yishay-at-bay opened this issue Jun 24, 2024 · 1 comment
Labels
documentation Improvements or additions to public interface documentation (API reference or readme).

Comments

@yishay-at-bay
Copy link

yishay-at-bay commented Jun 24, 2024

Hi

If I use React

useEffect(() => {
    const connectionChangeCallback = (
      connectionState: ConnectionStateChange,
    ) => {
      ...
    };

    client?.connection?.on(connectionChangeCallback);

    return () => client?.connection?.off(connectionChangeCallback);
  }, [client]);

Does that the right way to clean up that listener when the component unmounts?

┆Issue is synchronized with this Jira Task by Unito

@yishay-at-bay yishay-at-bay changed the title Do I need to clean up somehow connection listeners? Right way to clean up listeners Jun 24, 2024
@VeskeR
Copy link
Contributor

VeskeR commented Jun 27, 2024

Hi @yishay-at-bay !

Yes, this is the correct way to clean up connection events listeners when component unmounts.

You can also take a look at useEventListener hook from our ably/react module for some inspiration if you would like to extract that bit of code into a reusable hook for your application.

@VeskeR VeskeR added the documentation Improvements or additions to public interface documentation (API reference or readme). label Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to public interface documentation (API reference or readme).
Development

No branches or pull requests

2 participants