Skip to content

Commit 623cbe2

Browse files
Copilotalexr00
andcommitted
Fix useEffect dependency issue with ref
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent e9e5ee1 commit 623cbe2

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

webviews/editorWebview/overview.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ const STICKY_THRESHOLD_BUFFER = 10;
3535
export const Overview = (pr: PullRequest) => {
3636
const isSingleColumnLayout = useMediaQuery('(max-width: 768px)');
3737
const [isSticky, setIsSticky] = React.useState(false);
38+
const isStickyRef = React.useRef(isSticky);
39+
40+
// Keep ref in sync with state
41+
React.useEffect(() => {
42+
isStickyRef.current = isSticky;
43+
}, [isSticky]);
3844

3945
React.useEffect(() => {
4046
let ticking = false;
@@ -43,12 +49,13 @@ export const Overview = (pr: PullRequest) => {
4349
if (!ticking) {
4450
window.requestAnimationFrame(() => {
4551
const scrollY = window.scrollY;
52+
const currentSticky = isStickyRef.current;
4653
// Use hysteresis to prevent flickering at the threshold
4754
// When not sticky, activate when scrollY > threshold
4855
// When sticky, deactivate when scrollY < (threshold - buffer)
49-
if (!isSticky && scrollY > STICKY_THRESHOLD) {
56+
if (!currentSticky && scrollY > STICKY_THRESHOLD) {
5057
setIsSticky(true);
51-
} else if (isSticky && scrollY < STICKY_THRESHOLD - STICKY_THRESHOLD_BUFFER) {
58+
} else if (currentSticky && scrollY < STICKY_THRESHOLD - STICKY_THRESHOLD_BUFFER) {
5259
setIsSticky(false);
5360
}
5461
ticking = false;
@@ -59,7 +66,7 @@ export const Overview = (pr: PullRequest) => {
5966

6067
window.addEventListener('scroll', handleScroll, { passive: true });
6168
return () => window.removeEventListener('scroll', handleScroll);
62-
}, [isSticky]);
69+
}, []);
6370

6471
return <>
6572
<div id="title" className={`title ${isSticky ? 'sticky' : ''}`}>

0 commit comments

Comments
 (0)