Skip to content

Commit 418e4c3

Browse files
authored
Merge pull request #15 from NightCafeStudio/bugfix/memory-leak
Fix memory leak, keep local intersection ref
2 parents a5b942c + b6956e0 commit 418e4c3

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export const MyItemList = (items) => (
9292
</table>
9393
)
9494
```
95+
9596
The example above, builds a valid HTML table like the one shown below:
9697
```
9798
<table class="my-list">

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"scripts": {
1414
"build": "rollup -c",
1515
"start": "rollup -c -w",
16-
"prettier": "prettier --write ./src"
16+
"prettier": "prettier --write ./src",
17+
"publish": "np"
1718
},
1819
"dependencies": {},
1920
"peerDependencies": {

src/render-if-visible.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ const RenderIfVisible = ({
4343
// Set visibility with intersection observer
4444
useEffect(() => {
4545
if (intersectionRef.current) {
46+
const localRef = intersectionRef.current
4647
const observer = new IntersectionObserver(
4748
(entries) => {
4849
// Before switching off `isVisible`, set the height of the placeholder
4950
if (!entries[0].isIntersecting) {
50-
placeholderHeight.current = intersectionRef.current!.offsetHeight
51+
placeholderHeight.current = localRef!.offsetHeight
5152
}
5253
if (typeof window !== undefined && window.requestIdleCallback) {
5354
window.requestIdleCallback(
@@ -62,10 +63,11 @@ const RenderIfVisible = ({
6263
},
6364
{ root, rootMargin: `${visibleOffset}px 0px ${visibleOffset}px 0px` }
6465
)
65-
observer.observe(intersectionRef.current)
66+
67+
observer.observe(localRef)
6668
return () => {
67-
if (intersectionRef.current) {
68-
observer.unobserve(intersectionRef.current)
69+
if (localRef) {
70+
observer.unobserve(localRef)
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)