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

No values reported when using React StrictMode #90

Closed
fabianishere opened this issue Apr 14, 2022 · 5 comments
Closed

No values reported when using React StrictMode #90

fabianishere opened this issue Apr 14, 2022 · 5 comments
Labels
bug Something isn't working react-18 released

Comments

@fabianishere
Copy link

Hi,

I am using this hook in one of my projects in conjunction with React 18 and Strict Mode. However, it appears that the hook does not report any values when used together with React Strict Mode.

See the following CodeSandbox for a demonstration of this issue.

This issue is caused by the fact that React Strict Mode renders components twice in development mode in order to detect problems. In particular, this behavior causes problems with the following logic (for tracking unmounts):

useEffect(() => {
return () => {
didUnmount.current = true;
};
}, []);

Because the component is rendered twice, the component is marked as unmounted and no further size updates are reported by the hook.

I believe the following patch should resolve this issue:

  const didUnmount = useRef(false);
  useEffect(() => {
+   didUnmount.current = false;
    return () => {
      didUnmount.current = true;
    };
  }, []);
@ZeeCoder ZeeCoder added bug Something isn't working react-18 labels Apr 20, 2022
@ZeeCoder
Copy link
Owner

Thank you for the reproduction and the detailed report. 🙏
I plan to do a release soon, just have to update the tests so that they run for at least React 17 and 18 to ensure compatibility.

@fabianishere
Copy link
Author

Awesome, let me know if I can be of any help :)

@SawasReaper
Copy link

Any update on that one? ^^ 🙏

@ZeeCoder
Copy link
Owner

Hopefully I'll have some time this weekend to wrap things up 🤞

github-actions bot pushed a commit that referenced this issue May 15, 2022
# [9.0.0](v8.0.0...v9.0.0) (2022-05-15)

### Bug Fixes

* Added some fixes for React 18. ([852d976](852d976)), closes [#90](#90) [#91](#91) [#92](#92)

### BREAKING CHANGES

* The lib now takes "Element", not only "HTMLElement", to
be consistent with ResizeObserver.
@github-actions
Copy link

🎉 This issue has been resolved in version 9.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working react-18 released
Projects
None yet
Development

No branches or pull requests

3 participants