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.

The "onResize" callback doesn't work correctly with conditional rendering

See original GitHub issue

I’m using the library with onResize callback because I need to use another reference to DOM element to set width, but I have an issue when the component uses “loading status” so component with associated ref is conditionally rendered.

  return (
    <>
      {displayLoader ? (
        <LoadingBar />
      ) : (
        <Grid ref={measuredBoxRef}>...</Grid>
      )}
    </>
  )

In the library is a dependency for ref in the useEffect hook so when loading state is changed and component is rendered the ResizeObserver doesn’t observe this element because the ref is not completely changed (only current value). This problem is described on pages: https://medium.com/@teh_builder/ref-objects-inside-useeffect-hooks-eb7c15198780 https://dev.to/fnky/pitfalls-of-conditional-rendering-and-refs-in-react-29oi

I can’t separate the component like it is proposed in second article, so I’m trying to change the library to use useCallback hook.

Perhaps you met with this problem and you have another solution?

I know that issue is specified for my code but ref in dependencies is not recommended practice and even on the React page is an example to measure DOM element with useCallback hook: https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ZeeCodercommented, Apr 26, 2020

Yeah so the useCallback hook is not even the interesting part there, but rather supporting callback refs, so that the observer hook would receive the new ref as React provides it.

Not sure what it would look like though…

Maybe something like this?

const { width, height, callbackRef } = useResizeObserver<HTMLDivElement>();

return <div ref={callbackRef}></div>

Then whenever a new node is provided, the hook would trigger the same effect it currently does when the ref instance changes.

Could work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rerender view on browser resize with React
Using React Hooks: You can define a custom Hook that listens to the window resize event, something like this: import React, { useLayoutEffect,...
Read more >
Observer is constantly destroyed and replaced when using ...
Conceptually, the onResize callback is completely static between renders of the component. But here's the kicker: two functions are equal if ...
Read more >
Re-render a React Component on Window Resize
This code will simply listen for the window resize event and console log something like "resized to: 1024 x 768". Re-render on Resize....
Read more >
ResizeObserver: it's like document.onresize for elements
ResizeObserver is no exception. You create a ResizeObserver object and pass a callback to the constructor.
Read more >
How to use ResizeObserver with Angular
This happens when the viewport changes but our component doesn't. Do you see the first window.onresize (colored in red)? We are not interested ......
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