【问题标题】:React not showing updated state in child componentsReact 未在子组件中显示更新状态
【发布时间】:2017-04-16 23:33:52
【问题描述】:

这是对React tutorial 的改编。 我的意图是使用一两个玩家的选择来迁移到自动 动作的选择。在这个阶段,由于某种原因,正方形的选择不是 在使用this.setState({squares:this.state.squares, xIsNext: !this.state.xIsNext}) 语句对handleMove(i) 的调用中捕获,从 Button 和 Square 组件中的两个 console.log 调用。

这是本教程中使用的技术,所以我不确定为什么 squares 数组不是 更新。我阅读了其他帖子和 Facebook 的关于不变性的讨论,其中指出 该功能(在这种情况下是handleMove(i),对吗?)必须在状态更改之前完成 体现在整体状态上。其他组件以及整个 DOM 中对 console.log 的调用 不反映方块上的点击。

谁能告诉我为什么这不起作用?谢谢你。

开启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>

【问题讨论】:

    标签: javascript reactjs state


    【解决方案1】:

    选择被捕获,但您没有在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

    【讨论】:

    • 太棒了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多