Skip to content

Commit 23a527f

Browse files
authored
Cancel animation frame on component unmount to avoid memory leaks (#1598)
1 parent 82c3a86 commit 23a527f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/table/src/TableVirtualBody.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo, useState, useEffect } from 'react'
1+
import React, { memo, useState, useEffect, useRef } from 'react'
22
import VirtualList from '@segment/react-tiny-virtual-list'
33
import debounce from 'lodash.debounce'
44
import PropTypes from 'prop-types'
@@ -29,6 +29,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {
2929
const [paneRef, setPaneRef] = useState()
3030
const [isIntegerHeight, setIsIntegerHeight] = useState(false)
3131
const [calculatedHeight, setCalculatedHeight] = useState(0)
32+
const requestId = useRef(null)
3233

3334
const updateOnResize = () => {
3435
autoHeights = []
@@ -52,7 +53,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {
5253
}
5354

5455
// When height is still 0 (or paneRef is not valid) try recursively until success.
55-
requestAnimationFrame(() => {
56+
requestId.current = requestAnimationFrame(() => {
5657
updateOnResize()
5758
})
5859
}
@@ -80,6 +81,7 @@ const TableVirtualBody = memo(function TableVirtualBody(props) {
8081

8182
return () => {
8283
window.removeEventListener('resize', onResize)
84+
cancelAnimationFrame(requestId.current)
8385
}
8486
}, [])
8587

0 commit comments

Comments
 (0)