最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property '0' of undefined - stuck with reactjs tutorial - Stack Overflow

programmeradmin2浏览0评论

I stuck at the "Storing a history" part of the tutorial, trying to pull state up from Board to Game. I've removed constructor from Board and was trying to change Board so that it takes squares via props:

 renderSquare(i) {
    return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />;
  }

but it fails..

code:

I stuck at the "Storing a history" part of the tutorial, trying to pull state up from Board to Game. I've removed constructor from Board and was trying to change Board so that it takes squares via props:

 renderSquare(i) {
    return <Square value={this.props.squares[i]} onClick={() => this.props.onClick(i)} />;
  }

but it fails..

code: https://codepen.io/gka/pen/eBgapz

Share Improve this question edited Nov 21, 2016 at 10:31 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Nov 20, 2016 at 17:04 GiedriusGiedrius 1381 silver badge8 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

In your example, you pass in squares as props. So you need to change

renderSquare(i) {
    return (
      <Square 
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />;
    );
}

into

renderSquare(i) {
    return (
      <Square 
        value={this.props[i]}
        onClick={() => this.props.onClick(i)}
      />;
    );
}

Because this.props already refers to the squares you passed in.

I had my App render Board instead of Game, so that was the reason it wasn't working.

发布评论

评论列表(0)

  1. 暂无评论