You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If elementRef.current within useElementSize starts out as null on initial render (typically because the target element does not render), useElementSize will not update and return the correct element size once elementRef.current changes to a non-null value.
I'm using useElementSize in a full screen dialog component similar to the simplified one above. The dialog component starts out as not open (open=false), so on first render, it returns null. This means that the dialog header element I want the size of doesn't render at all on initial render and therefore, elementRef.current is null. The useLayoutEffect within useElementSize runs and calls handleSize, but because elementRef.current is null, setSize does not get called and so useElementSize returns { width: 0, height: 0 }. This is all working as expected so far.
Then, the user clicks a button to open the dialog. So my dialog component re-renders, but now with open=true so it renders to the actual elements of the dialog component. Now elementRef.current becomes an actual node. However, there is nothing that causes handleSize to get called again, so useElementSize will always return { width: 0, height: 0 }. (Note: resizing the window does cause handleSize to get called and then useElementSize returns the correct element size.)
Suggested fix
I believe the fix is simple: add elementRef?.current to the item list in the call to useLayoutEffect. Then, on a subsequent re-render of useElementSize, when elementRef?.current changes from null to non-null, handleSize will get called and the size will be updated.
Summary
If
elementRef.current
withinuseElementSize
starts out asnull
on initial render (typically because the target element does not render),useElementSize
will not update and return the correct element size onceelementRef.current
changes to a non-null value.Simplified code example
Details
I'm using
useElementSize
in a full screen dialog component similar to the simplified one above. The dialog component starts out as not open (open=false
), so on first render, it returnsnull
. This means that the dialog header element I want the size of doesn't render at all on initial render and therefore,elementRef.current
isnull
. TheuseLayoutEffect
withinuseElementSize
runs and callshandleSize
, but becauseelementRef.current
isnull
,setSize
does not get called and souseElementSize
returns{ width: 0, height: 0 }
. This is all working as expected so far.Then, the user clicks a button to open the dialog. So my dialog component re-renders, but now with
open=true
so it renders to the actual elements of the dialog component. NowelementRef.current
becomes an actual node. However, there is nothing that causeshandleSize
to get called again, souseElementSize
will always return{ width: 0, height: 0 }
. (Note: resizing the window does causehandleSize
to get called and thenuseElementSize
returns the correct element size.)Suggested fix
I believe the fix is simple: add
elementRef?.current
to the item list in the call touseLayoutEffect
. Then, on a subsequent re-render ofuseElementSize
, whenelementRef?.current
changes fromnull
to non-null,handleSize
will get called and the size will be updated.Other info
This could possibly be related to #32.
The text was updated successfully, but these errors were encountered: