尝试从React JS构建这个教程游戏,但无法继续前进。

huangapple go评论73阅读模式
英文:

trying to build this tutorial game from react js and not able to move forward

问题

以下是您的代码的翻译部分:

import { useState } from "react";

function Square({ value, onSquareClick }) {
    return (
        <button className="square" onClick={onSquareClick}>{value}</button>
    );
}

function CreateSquares(squares, handleClick) {

    const data = [];
    for (let i = 0; i < 3; i++) {
        const rows = [];
        for (let j = 0; j < 3; j++) {
            rows.push(
                <Square value={squares[j]} onSquareClick={() => handleClick(j)} key={j} />
            );
        }
        data.push(
            <div className="board-row" key={i}>{rows}</div>
        );
    }
    return data;
}

function Board({ xIsNext, squares, onPlay }) {

    let winner = calculateWinner(squares);

    function handleClick(index) {
        if (squares[index] || winner) {
            return;
        }
        const nextSquares = squares.slice();

        nextSquares[index] = xIsNext ? 'X' : 'O';

        onPlay(nextSquares);
    }
    let status = winner ? "Winner: " + winner : "Next Player: " + (xIsNext ? "X" : "O");

    return (
        <>
            <div className="status">{status}</div>
            <CreateSquares squares={squares} handleClick={handleClick} />
            {/* 
            <div className="board-row">
                <Square value={squares[0]} onSquareClick={() => handleClick(0)} />
                <Square value={squares[1]} onSquareClick={() => handleClick(1)} />
                <Square value={squares[2]} onSquareClick={() => handleClick(2)} />
            </div>
            <div className="board-row">
                <Square value={squares[3]} onSquareClick={() => handleClick(3)} />
                <Square value={squares[4]} onSquareClick={() => handleClick(4)} />
                <Square value={squares[5]} onSquareClick={() => handleClick(5)} />
            </div>
            <div className="board-row">
                <Square value={squares[6]} onSquareClick={() => handleClick(6)} />
                <Square value={squares[7]} onSquareClick={() => handleClick(7)} />
                <Square value={squares[8]} onSquareClick={() => handleClick(8)} />
            </div>
            */}
        </>
    );
}

export default function Game() {
    const [history, setHistory] = useState([Array(9).fill(null)]);
    const [currentMove, setCurrentMove] = useState(0);
    const xIsNext = currentMove % 2 === 0;
    const currentSquares = history[currentMove];

    function handlePlay(nextSquares) {
        const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
        setHistory(nextHistory);
        setCurrentMove(nextHistory.length - 1);
    }

    function jumpTo(nextMove) {
        setCurrentMove(nextMove);
    }

    const moves = history.map((squares, move) => {
        let description = move > 0 ? 'Go to move #' + move : "Go to game start";

        return (
            <li key={move}>
                <button onClick={() => { jumpTo(move) }}>{description}</button>
            </li>
        );
    });

    return (
        <div className="game">
            <div className="game-board">
                <Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} />
            </div>
            <div className="game-info">
                <ol>{moves}</ol>
            </div>
        </div>
    );
}

function calculateWinner(squares) {
    const lines = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6]
    ];

    let len = lines.length;
    for (let i = 0; i < len; i++) {
        const [a, b, c] = lines[i];
        if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
            return squares[a];
        }
    }

    return null;
}

关于您的错误信息,看起来问题可能出现在handleClick函数未被正确定义。请确保handleClick函数在CreateSquares组件内正确传递并定义。如果问题仍然存在,请检查函数名称是否拼写正确,并确保没有其他命名冲突。

英文:

this is my code so far by following official react js tutorial of making a tic-tac-toe game, I able to complete the game, but struct on the 1st challenge of create square using loop instead of hardcoding it, as you can see the comment part of code.

import { useState } from &quot;react&quot;;
function Square({ value, onSquareClick }) {
return (
&lt;button className=&quot;square&quot; onClick={onSquareClick} &gt;{value}&lt;/button&gt;
);
}
function CreateSquares(squares, handleClick) {
const data = [];
for (let i = 0; i &lt; 3; i++) {
const rows = [];
for (let j = 0; j &lt; 3; j++) {
rows.push(
&lt;Square value={squares[j]} onSquareClick={() =&gt; handleClick(j)} key={j} /&gt;
);
}
data.push(
&lt;div className=&quot;board-row&quot; key={i}&gt;{rows}&lt;/div&gt;
);
}
return data;
}
function Board({ xIsNext, squares, onPlay }) {
let winner = calculateWinner(squares);
function handleClick(index) {
if (squares[index] || winner) {
return;
}
const nextSquares = squares.slice();
nextSquares[index] = xIsNext ? &#39;X&#39; : &#39;O&#39;;
onPlay(nextSquares);
}
let status = winner ? &quot;Winner: &quot; + winner : &quot;Next Player: &quot; + (xIsNext ? &quot;X&quot; : &quot;O&quot;);
return (
&lt;&gt;
&lt;div className=&quot;status&quot;&gt;{status}&lt;/div&gt;
&lt;CreateSquares squares={squares} handleClick={handleClick} /&gt;
{/* &lt;div className=&quot;board-row&quot;&gt;
&lt;Square value={squares[0]} onSquareClick={() =&gt; handleClick(0)} /&gt;
&lt;Square value={squares[1]} onSquareClick={() =&gt; handleClick(1)} /&gt;
&lt;Square value={squares[2]} onSquareClick={() =&gt; handleClick(2)} /&gt;
&lt;/div&gt;
&lt;div className=&quot;board-row&quot;&gt;
&lt;Square value={squares[3]} onSquareClick={() =&gt; handleClick(3)} /&gt;
&lt;Square value={squares[4]} onSquareClick={() =&gt; handleClick(4)} /&gt;
&lt;Square value={squares[5]} onSquareClick={() =&gt; handleClick(5)} /&gt;
&lt;/div&gt;
&lt;div className=&quot;board-row&quot;&gt;
&lt;Square value={squares[6]} onSquareClick={() =&gt; handleClick(6)} /&gt;
&lt;Square value={squares[7]} onSquareClick={() =&gt; handleClick(7)} /&gt;
&lt;Square value={squares[8]} onSquareClick={() =&gt; handleClick(8)} /&gt;
&lt;/div&gt; */}
&lt;/&gt;
);
}
export default function Game() {
const [history, setHistory] = useState([Array(9).fill(null)]);
const [currentMove, setCurrentMove] = useState(0);
const xIsNext = currentMove % 2 === 0;
const currentSquares = history[currentMove];
function handlePlay(nextSquares) {
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
}
function jumpTo(nextMove) {
setCurrentMove(nextMove);
}
const moves = history.map((squares, move) =&gt; {
let description = move &gt; 0 ? &#39;Go to move #&#39; + move : &quot;Go to game start&quot;;
return (
&lt;li key={move}&gt;
&lt;button onClick={() =&gt; { jumpTo(move) }}&gt;{description}&lt;/button&gt;
&lt;/li&gt;
);
});
return (
&lt;div className=&quot;game&quot;&gt;
&lt;div className=&quot;game-board&quot;&gt;
&lt;Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} /&gt;
&lt;/div&gt;
&lt;div className=&quot;game-info&quot;&gt;
&lt;ol&gt;{moves}&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
let len = lines.length;
for (let i = 0; i &lt; len; i++) {
const [a, b, c] = lines[i];
if (squares[a] &amp;&amp; squares[a] === squares[b] &amp;&amp; squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}

this is the error i'm getting and i'm not able to figure out how to fix this.

    handleClick is not a function
TypeError: handleClick is not a function
at onSquareClick (http://localhost:3000/main.9d076070d9e17e2c1932.hot-update.js:51:30)
at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:10826:18)
at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:10870:20)
at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:10927:35)
at invokeGuardedCallbackAndCatchFirstError (http://localhost:3000/static/js/bundle.js:10941:29)
at executeDispatch (http://localhost:3000/static/js/bundle.js:15085:7)
at processDispatchQueueItemsInOrder (http://localhost:3000/static/js/bundle.js:15111:11)
at processDispatchQueue (http://localhost:3000/static/js/bundle.js:15122:9)
at dispatchEventsForPlugins (http://localhost:3000/static/js/bundle.js:15131:7)
at http://localhost:3000/static/js/bundle.js:15291:16

i'm trying to learn react and struct here, I already studied official quick start guide 2-3 times, but still not able to figure out what i am doing wrong, any help will be appreciated

答案1

得分: 1

function CreateSquares(squares, handleClick) {
function checkClick(index) {
handleClick(index);
}
const data = [];
for (let i = 0; i < 3; i++) {
const rows = [];
for (let j = 0; j < 3; j++) {
const index = i * 3 + j; // Calculate a unique index for each square
rows.push(
<Square
value={squares[index]}
onSquareClick={() => handleClick(index)}
key={index}
/>
);
}
data.push(<div className="board-row" key={i}>{rows}</div>);
}
return data;
}
英文:
function CreateSquares(squares, handleClick) {
function checkClick(index) {
handleClick(index);
}
const data = [];
for (let i = 0; i &lt; 3; i++) {
const rows = [];
for (let j = 0; j &lt; 3; j++) {
rows.push(
&lt;Square value={squares[j]} onSquareClick={() =&gt; handleClick(j)} key={j} /&gt;
);
}
data.push(
&lt;div className=&quot;board-row&quot; key={i}&gt;{rows}&lt;/div&gt;
);
}
return data;
}

in this part of code i think you're incorrectly calling handleClick function ( i.e wrong index being passed , instead you have to pass unique index for each square based on rows and col ).

try this and let me know :

function CreateSquares(squares, handleClick) {
function checkClick(index) {
handleClick(index);
}
const data = [];
for (let i = 0; i &lt; 3; i++) {
const rows = [];
for (let j = 0; j &lt; 3; j++) {
const index = i * 3 + j; // Calculate a unique index for each square
rows.push(
&lt;Square
value={squares[index]}
onSquareClick={() =&gt; handleClick(index)}
key={index}
/&gt;
);
}
data.push(&lt;div className=&quot;board-row&quot; key={i}&gt;{rows}&lt;/div&gt;);
}
return data;
}

答案2

得分: 0

// 将 handleClick 函数移出 Board 组件
function handleClick() {
// 在这里添加你的代码逻辑
}

function Board() {
// Board 组件的代码

return (
// JSX 代码
);
}

// 为避免类型错误,将 handleClick 函数重命名
const handleClickOutsideBoard = handleClick;

// 从 Board 组件外部调用 handleClickOutsideBoard 函数
handleClickOutsideBoard();

英文:
// Move the handleClick function outside the Board component
function handleClick() {
// Your code logic here
}
function Board() {
// Board component code
return (
// JSX code
);
}
// Rename the handleClick function to avoid type errors
const handleClickOutsideBoard = handleClick;
// Call the handleClickOutsideBoard function from outside the Board component
handleClickOutsideBoard();

In this code, we moved the handleClick function outside the Board component so that it can be accessed from outside. We also renamed the function to handleClickOutsideBoard to avoid any potential type errors. You can then call the handleClickOutsideBoard function from anywhere outside the Board component.

答案3

得分: 0

需要传递以下内容:

<CreateSquares squares={squares} checkClick={handleClick} />

并且需要更改函数声明为:

function CreateSquares({ squares, checkClick }) {

请注意,这两个更改将确保正确传递squarescheckClick作为属性到CreateSquares组件中,并且在CreateSquares函数内可以正确地解构这些属性。

英文:

Made it working and here is the working solution

import { useState } from &quot;react&quot;;
function Square({ value, onSquareClick }) {
return (
&lt;button className=&quot;square&quot; onClick={onSquareClick} &gt;{value}&lt;/button&gt;
);
}
function CreateSquares({ squares, checkClick }) {
const data = [];
for (let i = 0; i &lt; 3; i++) {
const rows = [];
for (let j = 0; j &lt; 3; j++) {
const index = i * 3 + j; // Calculate a unique index for each square
rows.push(
&lt;Square value={squares[index]} onSquareClick={() =&gt; checkClick(index)} key={index} /&gt;
);
}
data.push(
&lt;div className=&quot;board-row&quot; key={i}&gt;{rows}&lt;/div&gt;
);
}
return data;
}
function Board({ xIsNext, squares, onPlay }) {
let winner = calculateWinner(squares);
function handleClick(index) {
if (squares[index] || winner) {
return;
}
const nextSquares = squares.slice();
nextSquares[index] = xIsNext ? &#39;X&#39; : &#39;O&#39;;
onPlay(nextSquares);
}
let status = winner ? &quot;Winner: &quot; + winner : &quot;Next Player: &quot; + (xIsNext ? &quot;X&quot; : &quot;O&quot;);
return (
&lt;&gt;
&lt;div className=&quot;status&quot;&gt;{status}&lt;/div&gt;
&lt;CreateSquares squares={squares} checkClick={handleClick} /&gt;
&lt;/&gt;
);
}
export default function Game() {
const [history, setHistory] = useState([Array(9).fill(null)]);
const [currentMove, setCurrentMove] = useState(0);
const xIsNext = currentMove % 2 === 0;
const currentSquares = history[currentMove];
function handlePlay(nextSquares) {
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
}
function jumpTo(nextMove) {
setCurrentMove(nextMove);
}
const moves = history.map((squares, move) =&gt; {
let description = move &gt; 0 ? &#39;Go to move #&#39; + move : &quot;Go to game start&quot;;
return (
&lt;li key={move}&gt;
&lt;button onClick={() =&gt; { jumpTo(move) }}&gt;{description}&lt;/button&gt;
&lt;/li&gt;
);
});
return (
&lt;div className=&quot;game&quot;&gt;
&lt;div className=&quot;game-board&quot;&gt;
&lt;Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} /&gt;
&lt;/div&gt;
&lt;div className=&quot;game-info&quot;&gt;
&lt;ol&gt;{moves}&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
let len = lines.length;
for (let i = 0; i &lt; len; i++) {
const [a, b, c] = lines[i];
if (squares[a] &amp;&amp; squares[a] === squares[b] &amp;&amp; squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}

I needed to pass

&lt;CreateSquares squares={squares} checkClick={handleClick} /&gt;

and need to do this

function CreateSquares({ squares, checkClick }) {

instead of this

function CreateSquares( squares, checkClick ) {

huangapple
  • 本文由 发表于 2023年5月28日 13:43:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定