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.

width and height undefined if "ref div" is located after conditional rendering

See original GitHub issue

Hi Masianok,

thanks for the library.

I have the following problem:

const [result, reexecuteQuery] = useGetMissionsTimeLineQuery({
    variables: {
      id: "7d99fb36-936b-4f40-81e6-0cbfce5190da",
      isActive: true,
      memberId: "xxxx",
    },
  });
  const { data, error, fetching } = result;
  const onResize = useCallback(
    () => {
       console.log('resize')
    },
    [],
  )

  const { width, height,ref } = useResizeDetector({onResize});
...
return <div ref={ref}>{`${width}x${height}`}</div>
`

work accordingly description.

But if I add: conditions before the final div:

  if (!data) return <div>Loading</div>;
  if (fetching) return <div>Loading</div>;
  if (error) return <div>error</div>;
  return <div ref={ref}>{`${width}x${height}`}</div>

witdhand height are undefined for ever.

Is it a bug, or did I use the library wrong ? Thanks for your help.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
barbalexcommented, Dec 10, 2021

This is pretty nerve racking. I have tried all kinds of settings (refreshMode, -rate, -Options). Without any I get undefined and it stays that way. With

{
    refreshMode: 'debounce',
    refreshRate: 300,
    refreshOptions: { trailing: true },
  }

the component renders twice after receiving the width. But id does return a width value.

So:

  1. does not work without options
  2. renders twice instead of once

I have conditional rendering too. But removing it seemed to change nothing.

1reaction
Capsicum42commented, Sep 12, 2021

Thank for the info. I corrected the links.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the size (height/width) of a specific instance of a ...
I am trying to simply get the width & height of one of the rendered <div/> on Child.js . This is a great...
Read more >
Input Components - React-admin - Marmelab
Input components must be used inside a Form element (e.g. <Form> , <SimpleForm> ... If true , the input will expand to fill...
Read more >
Testing element dimensions without the browser
current value starts as null , I can't compute the width on the initial render. To solve this problem, I used the onMouseEnter...
Read more >
Passing Props to a Component - React Docs
The default value is only used if the size prop is missing or if you pass size={undefined} . But if you pass size={null}...
Read more >
Making Sense of react-spring - CSS-Tricks
useHeight gives us a ref and a height value of the content we're measuring ... Since a modal is conditionally rendered based on...
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