初时React

根据官网做的小游戏。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

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

class Board extends React.Component {
  // constructor(props) {
  //   super(props)
  //   this.state = {
  //     squares: Array(9).fill(null),
  //     xIsNext: true
  //   }
  // }
  renderSquare(i) {
    return (
      <Square 
        value={this.props.squares[i]}
        onClickTest={() => this.props.onTopClick(i)}  
      />
    );
  }
  // handleClickTest(i) {
  //   const squares = this.state.squares.slice()
  //   if (calculationWinner(squares) || squares[i]) {
  //     return
  //   }
  //   squares[i] = this.state.xIsNext ? 'X' : 'O'
  //   this.setState({
  //     squares: squares,
  //     xIsNext: !this.state.xIsNext
  //   })
  // }
  render() {
    // const winner = calculationWinner(this.state.squares)
    // let status
    // if(winner) {
    //   status = 'Winner is ' + winner
    // } else {
    //   status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O')
    // }

    return (
      <div>
        {/* <div className="status">{status}</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)
        }
      ],
      xIsNext: true,
      step: 0
    }
  }
  handleClickTest(i) {
    const history = this.state.history.slice(0, this.state.step + 1)
    const current = history[history.length-1]
    const squares = current.squares.slice()
    if(calculationWinner(squares) || squares[i]) {
      return
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O'
    this.setState(
      {
        history: history.concat([{
          squares: squares
        }]),
        xIsNext: !this.state.xIsNext,
        step: history.length
      }
    )
  }
  jumpTo(index) {
    this.setState({
      step: index,
      xIsNext: (index % 2) === 0
    })
  }
   render() {
    const history = this.state.history
    const current = history[this.state.step]
    const winner = calculationWinner(current.squares)
    let status
    if(winner) {
      status = `winner is ${winner}`
    } else {
      status = "next player is " + (this.state.xIsNext ? 'X' : 'O')
    }
    const ret = history.map((val,index)=>{
      const desc = index ? `Move to #${index}` : 'start new game'
      return (
        <li key={index}><button onClick={() => this.jumpTo(index)}>{desc}</button></li>
      )
    })
    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onTopClick={(i) => this.handleClickTest(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <ol>{ret}</ol>
        </div>
      </div>
    );
  }
}

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

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

function calculationWinner(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
}
原文地址:https://www.cnblogs.com/re-doc/p/14081789.html