I have been searching at lengths for an answer to this question, without success.
I would like to find the size of a React component, from another component. Is there another way to do it than the approaches I have listed below?
I will use the answer to dynamically get the height of my navbar from other components and hooks that need it.
Usecase examples
- Hooks that smooth-scroll to certain targets, adjusting for navbar height.
- Components that animate children when they are visible in the viewport, and therefore also need to adjust for navbar height.
What I have tried
- Attaching a
ref
to the navbar, then forwarding that around my entire application. That's a bit of a nightmare. - Adding an
id
to the navbar container, then finding it with vanilla js (getElementById). I understand from many other answers, that this is inferior to using aref
.