1

我正在尝试按照他们官方网站上的反应教程:https ://reactjs.org/tutorial/tutorial.html

唯一的区别是我试图通过仅使用功能组件来实现它,这是我在代码笔上的代码:

请参阅 Snedden Gonsalves ( @snedden-gonsalves ) 在CodePen上 的 Pen React 教程功能组件

总结;我有一个简单的正方形组件,它绘制单个正方形,一个绘制 3x3 正方形板的父板组件和一个维护状态的总体游戏组件,如教程中的历史、当前正方形、当前回合等。我目前将方块从父游戏组件发送到板组件,如下所示:

<Board
   onClick ={(i)=>{handleClick(i)}}
   squares = {state.history[state.history.length - 1]}

/>

handleClick 创建新的正方形状态并将其附加到我认为应该动态传递的历史数组中,因为我还传递了数组中的最后一个条目,但看起来它根本没有将最后一个条目传递给子组件。不知道发生了什么..

谢谢你的时间。

成分:

正方形:

 const Square = (props)=>{
        return(
            <button className="square" onClick={(e)=>props.onClick()}>
                {props.value}
            </button>
        )
    }

木板:

const Board = (props) => {
    const renderSquare = (i)=>{
        return <Square
            value={props.squares[i]}
            onClick={() => {
                props.onClick(i)
                }
            }
        />
    }
    return(
        <div>
            <div className="board-row">
                {renderSquare(0)}
                {renderSquare(1)}
                {renderSquare(2)}
            </div>
            <div className="board-row">
                {renderSquare(3)}
                {renderSquare(4)}
                {renderSquare(5)}
            </div>
            <div className="board-row">
                {renderSquare(6)}
                {renderSquare(7)}
                {renderSquare(8)}
            </div>
        </div>
    )
}

游戏:

const Game = () =>{
    let [state, setState] = useState({
        history:[{
            squares:Array(9).fill(null)
        }],
        isXNext:true,
        status:'Next player X'
    });

    useEffect(() =>{
        const currentSquares = state.history[state.history.length - 1].squares;
        const winner = calculateWinner(currentSquares);
        if(winner){
            setState( state => ({...state, status:'Winner is ' + winner, ended:true}));
        }
    },[state.history]);


    const handleClick = (i) =>{
        const currentSquares = state.history[state.history.length - 1].squares;

        if(state.ended || currentSquares[i])
            return;

        const newSquares = currentSquares.slice();
        newSquares[i] = state.isXNext?'X':'O';

        const newIsXNext = !state.isXNext;
        const newHistory = state.history.concat([
            {squares:newSquares}]);

        setState(state => ({...state,
            history: newHistory,
            isXNext: newIsXNext,
            status:'Next player ' + (newIsXNext?'X':'O')
        }));

    }

    return(
        <div className="game">
            <div className="game-board">
                <Board
                    onClick ={(i)=>{handleClick(i)}}
                    squares = {state.history[state.history.length - 1]}
                />
            </div>
            <div className="game-info">
                <div>{state.status}</div>
                <ol>{/* TODO */}</ol>
            </div>
        </div>
    );
}
4

1 回答 1

0

结果是它传递了更新的状态,我没有使用正确的键,这就是我修复它的方法:

<Board
    onClick ={(i)=>{handleClick(i)}}
    squares = {state.history[state.history.length - 1].squares}/>

工作代码笔:

请参阅 CodePen 上的Snedden Gonsalves ( @snedden-gonsalves )编写 的 Pen React 教程功能组件

于 2020-02-15T16:39:53.753 回答