Skip to content

Commit f90bbef

Browse files
committed
fix for: Bug: Uncleaned Timeout Causes React Warning
1 parent 2ef4d67 commit f90bbef

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/components/copyForLLMButton.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import {useState} from 'react';
3+
import {useEffect,useRef, useState} from 'react';
44
import {Check, Clipboard} from 'react-feather';
55
import * as Sentry from '@sentry/nextjs';
66

@@ -23,6 +23,15 @@ interface Props {
2323

2424
export function CopyForLLMButton({markdownPath}: Props) {
2525
const [copied, setCopied] = useState(false);
26+
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
27+
28+
useEffect(() => {
29+
return () => {
30+
if (timeoutRef.current) {
31+
clearTimeout(timeoutRef.current);
32+
}
33+
};
34+
}, []);
2635

2736
async function handleClick() {
2837
let didCopy = false;
@@ -51,7 +60,10 @@ export function CopyForLLMButton({markdownPath}: Props) {
5160
// Visual feedback only when something was actually copied
5261
if (didCopy) {
5362
setCopied(true);
54-
setTimeout(() => setCopied(false), 1200);
63+
if (timeoutRef.current) {
64+
clearTimeout(timeoutRef.current);
65+
}
66+
timeoutRef.current = setTimeout(() => setCopied(false), 1200);
5567
}
5668
}
5769

0 commit comments

Comments
 (0)