简体   繁体   English

反应棋盘与 chess.js 未捕获的 TypeError

[英]react-chessboard with chess.js uncaught TypeError

I am trying to implement react-chessboard with chess.js to my website so that I can evaluate a position that a user can create themselves, I am not trying to verify legal moves.我正在尝试将带有 chess.js 的 react-chessboard 实施到我的网站,以便我可以评估用户可以自己创建的 position,我不是要验证合法的动作。 Here is my code:这是我的代码:

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;

When I try to make a move on the webpage it gives this error: Uncaught TypeError: gameCopy.move is not a function.当我尝试在网页上移动时出现此错误:Uncaught TypeError: gameCopy.move is not a function。

The code is straight from the react-chessboard documentation so I'm not sure why there is an error.该代码直接来自 react-chessboard 文档,所以我不确定为什么会出现错误。

How can I fix this?我怎样才能解决这个问题?

Thank you谢谢

It seems it thinks that gameCopy.move is not a function, and you try to use it as such here: gameCopy.move(move);它似乎认为 gameCopy.move 不是 function,您尝试在此处这样使用它: gameCopy.move(move); . .

I am not familiar with the react-chessboard documentation, but have you made any changes to the code?我不熟悉 react-chessboard 文档,但是你对代码进行了任何更改吗? Most often if you have such function it is either that you have not initiated a gameCopy, or you have forgotten to import something.大多数情况下,如果您有这样的 function,那要么是您没有启动 gameCopy,要么是您忘记导入某些东西。

You're importing chess.js incorrectly I believe我相信你错误地导入了 chess.js

You're doing:你正在做的:

import {Chess} from 'chess.js';

The react-chessboard documentation shows:反应棋盘文档显示:

import Chess from "chess.js";

The dependencies for the example in react-chessboard show the chess.js version "^0.12.0", which you could install and the example would work as expected. react-chessboard 中示例的依赖项显示 chess.js 版本“^0.12.0”,您可以安装该版本,该示例将按预期工作。 At the time of writing this chess.js is on version "1.0.0-alpha.0", which presents the issue you described because the ongoing game is written as a class 'Chess', which you instantiate in the useState() definition.在撰写本文时,chess.js 的版本为“1.0.0-alpha.0”,它会出现您描述的问题,因为正在进行的游戏被编写为 class 'Chess',您在 useState() 定义中对其进行了实例化. When you spread {...game} in gameCopy you lose the class methods and only retain the variables.当您在 gameCopy 中传播 {...game} 时,您会丢失 class 方法并且只保留变量。

To integrate with the latest version of chess.js I would recommend storing the fen in state and updating that when valid moves are made.为了与最新版本的 chess.js 集成,我建议将 fen 存储在 state 中,并在进行有效移动时更新它。

const Board = () => {
    const [game] = useState(new Chess());
    const [fen, setFen] = useState(game.fen());

    const makeMove = (move) => {
        // this prevents invalid moves, since they would return null
        if(game.move(move)) {
            setFen(game.fen());
        }    
    }

    const onDrop = (startSquare, endSquare) => {
        makeMove({
            from: startSquare,
            to: endSquare,
        });
    }


  return <Chessboard position={fen} onPieceDrop={onDrop} />;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM