这是一篇记录,把 React 的官网的入门教程过一遍,这个项目非常好,官网最后没有贴出最终的代码,我把最后操作成功的代码贴下来了,可以多看看琢磨。

一、React 代码

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import './index.css'
  4. function Square(props) {
  5. return (
  6. <button className="square" onClick={props.onClick}>
  7. {props.value}
  8. </button>
  9. )
  10. }
  11. class Board extends React.Component {
  12. renderSquare(i) {
  13. return (
  14. <Square
  15. value={this.props.squares[i]}
  16. onClick={() => {
  17. this.props.onClick(i)
  18. }}
  19. />
  20. )
  21. }
  22. render() {
  23. return (
  24. <div>
  25. <div className="board-row">
  26. {this.renderSquare(0)}
  27. {this.renderSquare(1)}
  28. {this.renderSquare(2)}
  29. </div>
  30. <div className="board-row">
  31. {this.renderSquare(3)}
  32. {this.renderSquare(4)}
  33. {this.renderSquare(5)}
  34. </div>
  35. <div className="board-row">
  36. {this.renderSquare(6)}
  37. {this.renderSquare(7)}
  38. {this.renderSquare(8)}
  39. </div>
  40. </div>
  41. )
  42. }
  43. }
  44. class Game extends React.Component {
  45. constructor(props) {
  46. super(props)
  47. this.state = {
  48. history: [
  49. {
  50. squares: Array(9).fill(null)
  51. }
  52. ],
  53. xIsNext: true,
  54. stepNumber: 0
  55. }
  56. }
  57. handleClick(i) {
  58. const history = this.state.history.slice(0, this.state.stepNumber + 1)
  59. const current = history[history.length - 1]
  60. const squares = current.squares.slice()
  61. if (calculateWinner(squares) || squares[i]) {
  62. return
  63. }
  64. squares[i] = this.state.xIsNext ? 'X' : 'O'
  65. this.setState({
  66. history: history.concat([
  67. {
  68. squares: squares
  69. }
  70. ]),
  71. stepNumber: history.length,
  72. xIsNext: !this.state.xIsNext
  73. })
  74. }
  75. jumpTo(step) {
  76. this.setState({
  77. stepNumber: step,
  78. xIsNext: step % 2 === 0
  79. })
  80. }
  81. render() {
  82. const history = this.state.history
  83. const current = history[this.state.stepNumber]
  84. const winner = calculateWinner(current.squares)
  85. const moves = history.map((step, move) => {
  86. const desc = move ? 'Go to move #' + move : 'Go to game start'
  87. return (
  88. <li key={move}>
  89. <button onClick={() => this.jumpTo(move)}>{desc}</button>
  90. </li>
  91. )
  92. })
  93. let status
  94. if (winner) {
  95. status = 'Winner' + winner
  96. } else {
  97. status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O')
  98. }
  99. return (
  100. <div className="game">
  101. <div className="game-board">
  102. <Board
  103. onClick={(i) => {
  104. this.handleClick(i)
  105. }}
  106. squares={current.squares}
  107. />
  108. </div>
  109. <div className="game-info">
  110. <div>{status}</div>
  111. <ol>{moves}</ol>
  112. </div>
  113. </div>
  114. )
  115. }
  116. }
  117. // ========================================
  118. ReactDOM.render(<Game/>, document.getElementById('root'))
  119. function calculateWinner(squares) {
  120. const lines = [
  121. [0, 1, 2],
  122. [3, 4, 5],
  123. [6, 7, 8],
  124. [0, 3, 6],
  125. [1, 4, 7],
  126. [2, 5, 8],
  127. [0, 4, 8],
  128. [2, 4, 6]
  129. ]
  130. for (let i = 0; i < lines.length; i++) {
  131. const [a, b, c] = lines[i]
  132. if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
  133. return squares[a]
  134. }
  135. }
  136. return null
  137. }

二、参考链接

认识 React:https://react.docschina.org/tutorial/tutorial.html#making-an-interactive-component