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.

useResizeDetector with targetRef - version 1.3.1

See original GitHub issue

Hi, if have the following problem with the new targetRef: If I use: const {width, height} = useResizeDetector({targetRef: someRef}) width and height are always undefined for my component Whereas if I use: const {width, height} = useResizeDetector({targetRef: someRef, onResize: ()=>{}}) I get the correct dimensions of the someRef component,… which doesn’t really make sense to me. I suspect react-resize-detector is not reacting to ref changes properly. Adding ref.current to the dependency array [refreshMode, refreshRate, refreshOptions, handleWidth, handleHeight, onResize] (which helps in my case) seems to be wrong and “wrong” Any ideas? Cheers,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
maslianokcommented, Feb 7, 2021

Aha, I see…

You’re trying to implement the approach Dan mentioned in this comment , right?

Does it change the library API? I mean that you can’t pass targetRef anymore, right? Can you provide a code snippet of how it’s intended to be used?

0reactions
mungojamcommented, Nov 10, 2021

Similar to the above, I found that I was getting an initial delay before the width and height would get assessed. Using something in the current react docs, I was able to fix it by avoiding useRef altogether as they recommend:

    const [wrapperRef, setWrapperRef] = useState<
      MutableRefObject<unknown> | undefined
    >(undefined);

    const refCallback = useCallback((node) => {
      setWrapperRef({ current: node });
    }, []);

    const { width, height } = useResizeDetector({
      targetRef: wrapperRef,
    });

    return 
      <div ref={refCallback}>
	<MyBitsUsingWidthAndHeight />
      </div>

I wonder if you should do similar. I’ve asked if they can add an equivalent example to the beta docs, because I might be relying on some legacy ref thing here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useResizeDetector with targetRef - version 1.3.1 #130
onResize is an anonymous function. JS creates a new instance of this function every time the parent component rerenders. And, as a result,...
Read more >
A Cross-Browser, Event-based, Element Resize Detection for ...
useResizeDetector with targetRef - version 1.3.1. Hi, if have the following problem with the new targetRef: If I use: const {width, ...
Read more >
react-resize-detector - githubmemory
Warning useLayoutEffect in SSR · Return support to React 16^ · Stray console logs · useResizeDetector with targetRef - version 1.3.1.
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