Skip to content

Commit

Permalink
Cancel animation frame on component unmount to avoid memory leaks (#1598
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Arooba-git committed Jan 31, 2023
1 parent 82c3a86 commit 23a527f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/table/src/TableVirtualBody.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useState, useEffect } from 'react'
import React, { memo, useState, useEffect, useRef } from 'react'
import VirtualList from '@segment/react-tiny-virtual-list'
import debounce from 'lodash.debounce'
import PropTypes from 'prop-types'
Expand Down Expand Up @@ -29,6 +29,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {
const [paneRef, setPaneRef] = useState()
const [isIntegerHeight, setIsIntegerHeight] = useState(false)
const [calculatedHeight, setCalculatedHeight] = useState(0)
const requestId = useRef(null)

const updateOnResize = () => {
autoHeights = []
Expand All @@ -52,7 +53,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {
}

// When height is still 0 (or paneRef is not valid) try recursively until success.
requestAnimationFrame(() => {
requestId.current = requestAnimationFrame(() => {
updateOnResize()
})
}
Expand Down Expand Up @@ -80,6 +81,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {

return () => {
window.removeEventListener('resize', onResize)
cancelAnimationFrame(requestId.current)
}
}, [])

Expand Down

0 comments on commit 23a527f

Please sign in to comment.