handlePlyChange passed as prop to useEffect is being recreated every time state changes, so wrapper in useCallback

This commit is contained in:
2026-06-02 20:26:47 +02:00
parent 7d8f390a50
commit 1251921ea1
2 changed files with 11 additions and 5 deletions
+2 -2
View File
@@ -20,7 +20,7 @@ const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => {
scrollToMove: false, scrollToMove: false,
}); });
// set initial position - TODO change onto file load // set initial position - TODO change to on file load
// handlePlyChange({ // handlePlyChange({
// ply: 0, // ply: 0,
// fen: viewerRef.current?.curData().fen, // fen: viewerRef.current?.curData().fen,
@@ -78,7 +78,7 @@ const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => {
move.removeEventListener("touchstart", clickHandlerDelay); move.removeEventListener("touchstart", clickHandlerDelay);
}); });
}; };
}, [gamePgn]); }, [gamePgn, handlePlyChange]);
return <div ref={containerRef} className="lpv-board" />; return <div ref={containerRef} className="lpv-board" />;
}; };
+9 -3
View File
@@ -1,4 +1,10 @@
import { type ChangeEvent, useReducer, useRef, useState } from "react"; import {
type ChangeEvent,
useCallback,
useReducer,
useRef,
useState,
} from "react";
import type { IPosition } from "#/interfaces.ts"; import type { IPosition } from "#/interfaces.ts";
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts"; import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
import { downloadPDF } from "#/utils/pdfUtils.ts"; import { downloadPDF } from "#/utils/pdfUtils.ts";
@@ -17,9 +23,9 @@ export const useChessGame = () => {
const fileInputRef = useRef<HTMLInputElement | null>(null); const fileInputRef = useRef<HTMLInputElement | null>(null);
const handlePlyChange = ({ ply, fen }: IPosition) => { const handlePlyChange = useCallback(({ ply, fen }: IPosition) => {
setCurrentPosition({ ply, fen }); setCurrentPosition({ ply, fen });
}; }, []);
const handleSavePgn = () => { const handleSavePgn = () => {
const pgnString = buildPgnString(gameState); const pgnString = buildPgnString(gameState);