Skip to content

Commit bcc3c5e

Browse files
committed
Update PingPongGame.tsx
1 parent 0fbcf71 commit bcc3c5e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

docs/app/components/PingPongGame.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default function PingPongGame() {
6363
const canvasRef = useRef<HTMLCanvasElement>(null)
6464
const stateRef = useRef<State | null>(null)
6565
const rafRef = useRef<number>(0)
66+
const tickRef = useRef<() => void>(() => {})
6667
const wRef = useRef(560)
6768
const hRef = useRef(220)
6869
const reducedRef = useRef(false)
@@ -223,9 +224,11 @@ export default function PingPongGame() {
223224
}
224225

225226
draw(ctx, s, w, h)
226-
rafRef.current = requestAnimationFrame(tick)
227+
rafRef.current = requestAnimationFrame(tickRef.current)
227228
}, [draw])
228229

230+
useEffect(() => { tickRef.current = tick }, [tick])
231+
229232
// ── setup ───────────────────────────────────────────────────────────────────
230233
useEffect(() => {
231234
const canvas = canvasRef.current
@@ -261,7 +264,7 @@ export default function PingPongGame() {
261264
ro.observe(canvas)
262265

263266
if (!reducedRef.current) {
264-
rafRef.current = requestAnimationFrame(tick)
267+
rafRef.current = requestAnimationFrame(tickRef.current)
265268
}
266269

267270
// ── mouse ────────────────────────────────────────────────────────────────
@@ -306,7 +309,7 @@ export default function PingPongGame() {
306309
window.removeEventListener('keydown', onKeyDown)
307310
window.removeEventListener('keyup', onKeyUp)
308311
}
309-
}, [tick, draw])
312+
}, [draw])
310313

311314
return (
312315
<canvas

0 commit comments

Comments
 (0)