react-resize-observer-hook icon indicating copy to clipboard operation
react-resize-observer-hook copied to clipboard

No disconnecting when component unmounts.

Open tomisu opened this issue 6 years ago • 7 comments

While using this, I found this problem: if the component using this hook unmounts, React complains about a memory leak.

To fix this, resizeObserver should disconnect on useEffect's return function:

export const useResizeObserver = (ref: RefObject<HTMLElement>, callback: ObserverCallback) => {
  useEffect(() => {
    const resizeObserver = new (window as any).ResizeObserver((entries: ResizeObserverEntry[]) => {
      callback(entries[0].contentRect);
    });

    resizeObserver.observe(ref.current);

    return () => {
      resizeObserver.disonnect();
    }
  }, [ref]);
}

tomisu avatar Sep 17 '19 11:09 tomisu

Hi @tomisu ! Thanks for opening this.

What you are saying makes a lot of sense to me, would you like to submit a PR with that change? I will more than happy to merge it :)

zzarcon avatar Sep 17 '19 12:09 zzarcon

@zzarcon I don't seem to have permission to push into a new branch.ERROR: Permission to zzarcon/react-resize-observer-hook.git denied to tomisu.

tomisu avatar Sep 18 '19 08:09 tomisu

@tomisu You can create a fork and push the change to it. After that, you can create a pull request based on your fork (see GitHub's documentation on PR).

But since you are making a small change, there's an easier way to do this: Go to the file you want to change on GitHub, and click the pencil icon on the upper-right corner. After that, the website should guide you through the process.

yvt avatar Oct 03 '19 15:10 yvt

Thanks @yvt ! I didn't know that.

Here's the PR; I hope I did everything OK: https://github.com/zzarcon/react-resize-observer-hook/pull/4

tomisu avatar Oct 04 '19 07:10 tomisu

Isn't there a typo there? resizeObserver.disonnect(); <-missing c

Lanchez avatar Nov 05 '19 21:11 Lanchez

You're totally right, how embarrassing!

I crated a PR: https://github.com/zzarcon/react-resize-observer-hook/pull/6

Thanks!

tomisu avatar Nov 06 '19 08:11 tomisu

@zzarcon Since these PRs are all merged and it's been a while since then, can you publish a new version to npm?

yvt avatar Apr 02 '20 10:04 yvt