React 未在子组件中显示更新状态
React not showing updated state in child components
这是 React tutorial 的改编版。
我的意图是使用一两个玩家的选择来迁移到自动
动作的选择。在这个阶段,由于某种原因,方块的选择还没有
在使用 this.setState({squares:this.state.squares, xIsNext: !this.state.xIsNext})
语句调用 handleMove(i)
时捕获,从
Button 和 Square 组件中的两次 console.log 调用。
这是教程中使用的技术,所以我不确定为什么方块数组不是
更新。我阅读了其他帖子和 Facebook 关于不变性的讨论,其中指出
函数(在本例中 handleMove(i)
,正确吗?)必须在状态更改之前完成
反映在整体状态上。在其他组件中调用 console.log 以及整体 DOM
不反映在方块上的点击。
谁能告诉我为什么这不起作用?谢谢你。
在 JSBin
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Tic Tac Toe</title>
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id='root'></div>
<script type='text/babel' >
function Square(props){
console.log(props.value);
return(
<button className="square" onClick={() => props.onClick()}>{props.value}</button>
);
}
class Board extends React.Component{
renderSquare(i){
console.log(this.props.squares);
return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />
}
render(){
return(
<div className="board">
<div className="row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends React.Component{
constructor(props){
super(props);
this.state = {
onePlayerGame: true,
squares: Array(9).fill(null),
xIsNext: true
}
}
onePlayer(){
this.setState({onePlayerGame: true});
return(
document.getElementById('onePlayer').style.color = "yellow",
document.getElementById('twoPlayer').style.color = "black"
);
}
twoPlayer(){
this.setState({ onePlayerGame: false});
return(
document.getElementById('onePlayer').style.color= "black",
document.getElementById('twoPlayer').style.color= "yellow"
);
}
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: this.state.squares,
xIsNext: !this.state.xIsNext
})
}
render(){
return(
<div id="main">
<div>
<button className="gameSelect" id="onePlayer" value={this.state.onePlayerGame} onClick={() => this.onePlayer()} >One Player</button>
<button className="gameSelect" id="twoPlayer"value={this.state.onePlayerGame} onClick={() => this.twoPlayer()} >Two Players</button>
</div>
<Board
squares={this.state.squares}
onClick={(i) => this.handleMove(i)} />
</div>
);
}
}
ReactDOM.render(
<Game />,
document.getElementById('root')
)
</script>
</body>
</html>
选择已捕获,但您没有在 state
中保存 squares
的新值。
这个
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: this.state.squares, <----- You are not assigning the new value
xIsNext: !this.state.xIsNext
})
}
应该是
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext
})
}
这里是正确的jsbin
这是 React tutorial 的改编版。
我的意图是使用一两个玩家的选择来迁移到自动
动作的选择。在这个阶段,由于某种原因,方块的选择还没有
在使用 this.setState({squares:this.state.squares, xIsNext: !this.state.xIsNext})
语句调用 handleMove(i)
时捕获,从
Button 和 Square 组件中的两次 console.log 调用。
这是教程中使用的技术,所以我不确定为什么方块数组不是
更新。我阅读了其他帖子和 Facebook 关于不变性的讨论,其中指出
函数(在本例中 handleMove(i)
,正确吗?)必须在状态更改之前完成
反映在整体状态上。在其他组件中调用 console.log 以及整体 DOM
不反映在方块上的点击。
谁能告诉我为什么这不起作用?谢谢你。
在 JSBin
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Tic Tac Toe</title>
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id='root'></div>
<script type='text/babel' >
function Square(props){
console.log(props.value);
return(
<button className="square" onClick={() => props.onClick()}>{props.value}</button>
);
}
class Board extends React.Component{
renderSquare(i){
console.log(this.props.squares);
return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />
}
render(){
return(
<div className="board">
<div className="row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends React.Component{
constructor(props){
super(props);
this.state = {
onePlayerGame: true,
squares: Array(9).fill(null),
xIsNext: true
}
}
onePlayer(){
this.setState({onePlayerGame: true});
return(
document.getElementById('onePlayer').style.color = "yellow",
document.getElementById('twoPlayer').style.color = "black"
);
}
twoPlayer(){
this.setState({ onePlayerGame: false});
return(
document.getElementById('onePlayer').style.color= "black",
document.getElementById('twoPlayer').style.color= "yellow"
);
}
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: this.state.squares,
xIsNext: !this.state.xIsNext
})
}
render(){
return(
<div id="main">
<div>
<button className="gameSelect" id="onePlayer" value={this.state.onePlayerGame} onClick={() => this.onePlayer()} >One Player</button>
<button className="gameSelect" id="twoPlayer"value={this.state.onePlayerGame} onClick={() => this.twoPlayer()} >Two Players</button>
</div>
<Board
squares={this.state.squares}
onClick={(i) => this.handleMove(i)} />
</div>
);
}
}
ReactDOM.render(
<Game />,
document.getElementById('root')
)
</script>
</body>
</html>
选择已捕获,但您没有在 state
中保存 squares
的新值。
这个
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: this.state.squares, <----- You are not assigning the new value
xIsNext: !this.state.xIsNext
})
}
应该是
handleMove(i){
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X':'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext
})
}
这里是正确的jsbin