跟着react官网学习并制作了井字棋
遇到的一些问题:
Object is not avlidable
删掉标签中的 {}
Cannot access 'squares' before initialization
这是由于我的粗心导致的,我将square的声明放在了调用它的表达式的后面
修改后:
注意事项
· 添加构造函数来初始化state
· 在所有含有构造函数的的 React 组件中,构造函数必须以 super(props) 开头。
· 子组件更新父组件中的state方法:从父组件传入一个回调函数,子组件要改变时执行
功能实现
历史记录
这里使用了history数组来存储每次数据改变的状态
在 Game 中的 render 函数中使用map来遍历数组,并展示到 button 中
使用key来保证数据改动时的准确性
历史记录中,每一个历史步骤都有一个与之对应的唯一 ID:这个 ID 就是每一步棋的序号。因为历史步骤不需要重新排序、新增、删除,所以使用步骤的索引作为 key 是安全的
使用setpNumber记录步骤, 使用jumpTo跳转
在Game的state中添加一个setpNumber:0
代码➕注释
JS
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
/**
* 如果你想写的组件只包含一个 render 方法,并且不包含 state,
* 可以定义一个函数,这个函数接收 props 作为参数,然后返回需要渲染的元素。
*/
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
/* 传入state中的值 , 使用括号把react元素包裹起来防止js编译时破坏代码结构*/
return (
<Square
value={this.props.squares[i]}
/* 子组件更新父组件中的state方法:
从父组件传入一个回调函数,子组件要改变时执行
*/
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 {
/*
添加构造函数来初始化state
在所有含有构造函数的的 React 组件中,构造函数必须以 super(props) 开头。
*/
constructor(props) {
super(props);
this.state = {
/** 保存历史状态 */
history: [
{
squares: Array(9).fill(null),
},
],
/** 当前查看的历史步骤 */
stepNumber: 0,
xIsNext: true,
};
}
/* 更改state中的数据*/
handleClick(i) {
/** 并不是直接修改变量,而是创建了一个副本 */
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
/** 当有玩家胜出时,或者某个 Square 已经被填充时,该函数不做任何处理直接返回 */
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "0";
this.setState({
history: history.concat([
{
squares: squares,
},
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext,
});
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: step % 2 === 0,
});
}
render() {
const history = this.state.history;
/** 使用最新一次历史记录确定并展示状态 */
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
/** 展示历史步骤 */
const moves = history.map((step, move) => {
const desc = move ? "Go to move #" + move : "Go to game start";
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "Next player: " + (this.state.xIsNext ? "X" : "0");
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div> <ol>{moves}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(<Game />, document.getElementById("root"));
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;
}