@@ -76,6 +76,7 @@ export function VirtualElement({
7676 const isTransitioning = useRef ( false )
7777
7878 const isCurrentlyObserving = useRef ( false )
79+ const observedElementRef = useRef < HTMLDivElement | null > ( null )
7980 const isMountedRef = useRef ( true )
8081
8182 useEffect ( ( ) => {
@@ -117,6 +118,18 @@ export function VirtualElement({
117118 }
118119 } , [ ref , inView , placeholderHeight ] )
119120
121+ const unobserveElement = useCallback (
122+ ( element : HTMLDivElement | null ) => {
123+ if ( ! element ) return
124+ resizeObserverManager . unobserve ( element )
125+ if ( observedElementRef . current === element ) {
126+ observedElementRef . current = null
127+ }
128+ isCurrentlyObserving . current = false
129+ } ,
130+ [ resizeObserverManager ]
131+ )
132+
120133 // failsafe to ensure visible elements if resizing happens while scrolling
121134 useEffect ( ( ) => {
122135 if ( ! isShowingChildren ) {
@@ -171,6 +184,10 @@ export function VirtualElement({
171184 } , [ inView , isShowingChildren ] )
172185
173186 useEffect ( ( ) => {
187+ if ( observedElementRef . current && observedElementRef . current !== ref ) {
188+ unobserveElement ( observedElementRef . current )
189+ }
190+
174191 if ( inView ) {
175192 setIsShowingChildren ( true )
176193 }
@@ -201,14 +218,12 @@ export function VirtualElement({
201218 if ( ref ) {
202219 if ( ! isCurrentlyObserving . current ) {
203220 resizeObserverManager . observe ( ref , handleResize )
221+ observedElementRef . current = ref
204222 isCurrentlyObserving . current = true
205223 }
206224 }
207225 } else {
208- if ( ref ) {
209- resizeObserverManager . unobserve ( ref )
210- isCurrentlyObserving . current = false
211- }
226+ if ( ref ) unobserveElement ( ref )
212227 setIsShowingChildren ( false )
213228 }
214229 } catch ( error ) {
@@ -218,7 +233,7 @@ export function VirtualElement({
218233 inViewChangeTimerRef . current = undefined
219234 }
220235 } , 100 )
221- } , [ inView , ref , handleResize , resizeObserverManager ] )
236+ } , [ inView , ref , handleResize , resizeObserverManager , unobserveElement ] )
222237
223238 const onVisibleChanged = useCallback (
224239 ( visible : boolean ) => {
@@ -242,22 +257,23 @@ export function VirtualElement({
242257 // Setup initial observer if element is in view
243258 if ( ref && inView && ! isCurrentlyObserving . current ) {
244259 resizeObserverManager . observe ( ref , handleResize )
260+ observedElementRef . current = ref
245261 isCurrentlyObserving . current = true
246262 }
247263
248264 // Cleanup function
249265 return ( ) => {
250266 // Clean up resize observer
251- if ( ref ) {
252- resizeObserverManager . unobserve ( ref )
253- isCurrentlyObserving . current = false
267+ if ( ref ) unobserveElement ( ref )
268+ if ( observedElementRef . current && observedElementRef . current !== ref ) {
269+ unobserveElement ( observedElementRef . current )
254270 }
255271
256272 if ( inViewChangeTimerRef . current ) {
257273 clearTimeout ( inViewChangeTimerRef . current )
258274 }
259275 }
260- } , [ ref , inView , handleResize ] )
276+ } , [ ref , inView , handleResize , unobserveElement ] )
261277
262278 useEffect ( ( ) => {
263279 if ( inView === true ) {
0 commit comments