首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React JS打印用户的选择

React JS打印用户的选择
EN

Stack Overflow用户
提问于 2020-09-03 18:10:45
回答 1查看 64关注 0票数 0

我正在尝试React.js,我遵循了一个教程来制作一个morpion游戏。现在,我正在尝试获取用户的选择,并打印列和行。以下是我到目前为止所做的工作:

代码语言:javascript
复制
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

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

class Board extends React.Component {
    renderSquare(i) {
        return <Square
            value={this.props.squares[i]}
            onClick={ () => this.props.onClick(i)}
        />;
    }

    render() {
          return (
            <div>
                <div className="board-row">
                    {this.renderSquare(0)}
                    {this.renderSquare(1)}
                    {this.renderSquare(2)}
                </div>
                <div className="board-row">
                    {this.renderSquare(3)}
                    {this.renderSquare(4)}
                    {this.renderSquare(5)}
                </div>
                <div className="board-row">
                    {this.renderSquare(6)}
                    {this.renderSquare(7)}
                    {this.renderSquare(8)}
                </div>
            </div>
        );
    }
}

class Game extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            history: [{
                squares: Array(9).fill(null),
                choice: null,
            }],
            stepNumber: 0,
            xIsNext: true,
        };
    }

    jumpTo(step){
        this.setState({
            stepNumber: step,
            xIsNext: (step % 2) === 0,
        });
    }


    handleClick(i) {
        const history = this.state.history.slice(0, this.state.stepNumber + 1);
        const current = history[history.length - 1];
        const squares = current.squares.slice();

        //Si quelqu'un a gagné, on empêche le clic
        if (calculateWinner(squares) || squares[i]){
            return;
        }
        squares[i] = this.state.xIsNext ? 'X' : 'O';
        this.setState({
            history: history.concat([{
                squares: squares,
            }]),
            stepNumber: history.length,
            xIsNext: !this.state.xIsNext,
        });
    }

    render() {
        const history = this.state.history;
        const current = history[this.state.stepNumber];
        const winner = calculateWinner(current.squares);

        const choice = (current) => { this.calculateChoice(current.squares) };

        const moves = history.map((step, move) => {
            const desc = move ?
                'Revenir au tour n°' + move :
                'Revenir au début de la partie';
            return (
                <li key={move}>
                    <button onClick={() => this.jumpTo(move)}>{desc}</button>
                </li>
            );
        });

        let status;
        if (winner){
            status = winner + ' a gagné';
        } else {
            status = 'Prochain joueur : ' + (this.state.xIsNext ? 'X' : 'O');
        }

        return (
            <div className="game">
                <div className="game-board">
                    <Board
                        squares={current.squares}
                        onClick={ (i) => {
                            this.handleClick(i);
                        } }
                    />
                </div>
                <div className="game-info">
                    <div>Coup {choice}</div>
                    <div>{ status }</div>
                    <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],
    ];
    for (let i = 0; i < lines.length; i++){
        const [a, b, c] = lines[i];
        if(squares[a] && squares[a] === squares[b] && squares[a] === squares[c]){
            return squares[a];
        }
    }
    return null;
}

function calculateChoice(squares){
    const lines = [
        [0,0],[0,1],[0,2],
        [1,0],[1,1],[1,2],
        [2,0],[2,1],[2,2],
    ];
    for (let i = 0; i < lines.length; i++){
        const [a, b] = lines[i];
        return "test ".squares[a];
    }
    return "test";
}

// ========================================

ReactDOM.render(
    <Game />,
    document.getElementById('root')
);

我定义了一个常量选项,希望将用户的选择放在其中。然后我调用calculateChoice,但是当我尝试{ choice }时什么也没有显示。我也不知道原因。另外,我不知道如何获取用户点击的行和列。我对不同的选项做了一个标签,但我不知道如何将它们与用户点击的Square进行比较。谢谢

编辑:所以我改变了常量选择:

代码语言:javascript
复制
    const choice = calculateChoice(current.squares);

现在它起作用了,我得到了一个显示器。但是我不知道如何获取用户点击的列和行。我从calculateChoice开始,但我不知道我将从哪里开始。我想要的是:用户点击一个广场。'X‘或'O’会影响到Square,然后它会显示"choice“,它对应于用户点击的位置(行,列)。感谢您对如何开发它的任何解释!

EN

回答 1

Stack Overflow用户

发布于 2020-09-04 21:20:28

这是一个函数,const choice = (current) => { this.calculateChoice(current.squares) };

如果你呈现<div>Coup {choice}</div>,它当然不会呈现任何值,因为它是一个函数,,

  • ,这个函数不会返回正确值,因为它不打算返回任何东西。如果您想调用this.calculateChoice(current.squares),您可以执行const choice = (current) => { return this.calculateChoice(current.squares) };const choice = (current) => this.calculateChoice(current.squares);

  • If,确保它位于类组件内。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63721434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档