我正在尝试实现反应-棋盘与chess.js到我的网站,以便我可以评估一个位置,用户可以创建自己,我没有试图验证合法的举动。这是我的代码:
import React from 'react';
import {useState} from 'react';
import {Chessboard} from 'react-chessboard';
import {Chess} from 'chess.js';
const Board = () => {
const [game, setGame] = useState(new Chess());
const makeMove = (move) => {
const gameCopy = {...game};
gameCopy.move(move);
setGame(gameCopy);
return;
}
const onDrop = (startSquare, endSquare) => {
makeMove({
from: startSquare,
to: endSquare,
});
return;
}
return <Chessboard position={game.fen()} onPieceDrop={onDrop} />;
}
export default Board;当我试图在网页上移动时,它会产生这样的错误: Uncaught : gameCopy.move不是一个函数。
代码直接来自于react chessboard文档,所以我不确定为什么会出现错误。
我怎么才能解决这个问题?
谢谢
发布于 2022-10-27 08:40:41
它似乎认为gameCopy.move不是一个函数,您试图在这里使用它:gameCopy.move(move);。
我不熟悉react chessboard文档,但是您对代码做了任何更改吗?通常情况下,如果您有这样的函数,那就是您没有启动一个gameCopy,或者您忘记了导入什么。
发布于 2022-11-16 16:26:19
你不正确地导入了chess.js我相信
你在做:
import {Chess} from 'chess.js';
反应棋盘文件显示:
import Chess from "chess.js";
https://stackoverflow.com/questions/74188150
复制相似问题