question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No values reported when using React StrictMode

See original GitHub issue

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):

https://github.com/ZeeCoder/use-resize-observer/blob/653b0b98d5614b083835880736555b79536326aa/src/index.ts#L196-L200

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;
    };
  }, []);

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
github-actions[bot]commented, May 15, 2022

🎉 This issue has been resolved in version 9.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
ZeeCodercommented, May 13, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using strict mode in React 18: A guide to its new behaviors
Strict mode is trying to be future-ready with React's suspense-based architecture, making it more resilient for introspecting UI issues.
Read more >
Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates...
Read more >
Bug: React 18 Strict mode does not simulate unsetting and re ...
Refs that store use-managed values won't be reset when a component is hidden though. They allow things to be persisted for the case...
Read more >
My React Component is rendering twice because of Strict Mode
StrictMode renders components twice (on dev but not production) in order to detect any problems with your code and warn you about them ......
Read more >
What is StrictMode in React ? - GeeksforGeeks
StrictMode is a React Developer Tool primarily used for highlighting possible problems in a web application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found