Losing track of element with conditional rendering
See original GitHub issueBug Report
Describe the Bug
I have this element that I need to track its dimensions and react-cool-dimensions
works nicely for what I want to achieve, the problem is that this element is not always rendered and it looks like useDimensions
loses track of the element reference.
If that didn’t make sense, please see below…
How to Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Go to https://codesandbox.io/s/beautiful-haslett-sfhji
- A red box is loaded and its dimensions are reported correctly
- Click the
TOGGLE
button to switch to a blue box instead - The red box dimensions are now reported to be 0
- Click the
TOGGLE
button to switch back to the red box - The red box dimensions remain 0
Expected Behavior
The red box dimensions should be correctly reported in step 6 above.
Additional Information
- The red box is resizable if it helps to test this
- There’s another third-party hook commented out where this problem does not exist
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
why components state data are gone in conditional rendering
When I conditionally rendering , ie from false to true, the data inside child component will be gone. Then I wonder why is...
Read more >lightning web components - Error with Conditional Rendering
@JenniferPrather For now, it appears you need to check the "Aura" version of the docs. I'm not sure why the events are missing...
Read more >Conditional Rendering - React
You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the...
Read more >Increase Your Efficiency Using Lists and Conditional Rendering
The map() method allows you to easily loop through data and return an array of elements. In a similar vein, you'll find that...
Read more >5 Ways to Avoid React Component Re-Renderings
In this article, I have discussed 5 different methods to prevent unnecessary re-rendering in React components. Most of these solutions capitalize caching, and ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@wellyshen I finished looking at this and found a solution that worked for me 😃
Your
setRef
solution above was in the right direction but I had to change that to auseCallback
instead. However, that’s exactly what theobserve
is and I could just use that. But then things got tricky because I wanted to use the same reference within auseIntersection
hook (Intersection Observer) and wasn’t able to find a solution that worked. I even ended up replacing theuseIntersection
hook I was using withreact-cool-inview
(😉), but it didn’t fix the problem I was having (I’m still keeping your hook 😄).In the end, I realized that there was this container element that I could use with a bit of a refactor and so I did that. A solution like that (a container element around the conditional elements), prevents a lot of issues and makes things easier. If possible, of course, might not be possible for every use case, though.
I’d like to point out a few things I found while looking at this hook source code:
React.RefCallback<T>
index.ts
andreact-cool-dimensions.d.ts
. This is confusing and it’s usually no needed, the types are on the source already, and building a ES modules versions should output*.d.ts
files for you, no need to do it manuallyreact-cool-inview
react-cool-inview
but I’ll leave it here for now…root?: HTMLElement
- might be worth changing this type toElement
(HTMLElement
extends fromElement
) sincedocument.querySelector
returns anElement
(I’d imagine most people would use this to pick a root different from the default)Thank you for your support and kudos on these great hooks 😃
@rfgamaral Thanks bro it really helps.