简体   繁体   English

如何在反应中访问多维数组值?

[英]how to access multidimensional array value in react?

What I'm trying to do is to get value from 'coords' array and output it to webpage. 我想做的是从“坐标”数组中获取价值并将其输出到网页。 Other components are rendering fine. 其他组件也很好。 Right now I'm getting an error: 现在我遇到一个错误:

TypeError: Cannot read property '0' of undefined TypeError:无法读取未定义的属性“ 0”

Do I have some problems with syntax or I simply cant reach coords array? 我在语法上有问题吗?还是无法到达coords数组?

Here is the full code of component 这是组件的完整代码

class Ships extends Component{
//get random integer from min to max value
getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }


LShip(){

var startXCoord =  this.getRandomInt(0,9);
var startYCoord =  this.getRandomInt(0,9);
var coords = [];
//orientation will be chosen randomly
var orientation = this.getRandomInt(1,4);
switch (orientation) {
  case 1:
    return(
 this.setState.coords =[
          {'xcoord':this.startXCoord,'ycoord':this.startYCoord},
          {'xcoord':this.startXCoord+1,'ycoord':this.startYCoord},
          {'xcoord':this.startXCoord + 2,'ycoord':this.startYCoord},
          {'xcoord':this.startXCoord,'ycoord':this.startYCoord+1}
        ]
    )
    break;

  case 2:
  return(
  this.setState.coords = [
          {'xcoord':this.startXCoord,'ycoord':this.startYCoord},
         {'xcoord':this.startXCoord+1,'ycoord':this.startYCoord},
         {'xcoord':this.startXCoord + 2,'ycoord':this.startYCoord},
         {'xcoord':this.startXCoord+1,'ycoord':this.startYCoord}
        ]
        )
    break;

  case 3:
  return (
  this.setState.coords =[
           {'xcoord':this.startXCoord,'ycoord':this.startYCoord},
         {'xcoord':this.startXCoord,'ycoord':this.startYCoord+1},
         {'xcoord':this.startXCoord,'ycoord':this.startYCoord + 2},
         {'xcoord':this.startXCoord+1,'ycoord':this.startYCoord}
       ]
     )
    break;

  case 4:
  return(
  this.setState.coords =[
           {'xcoord':this.startXCoord,'ycoord':this.startYCoord},
           {'xcoord':this.startXCoord,'ycoord':this.startYCoord+1},
           {'xcoord':this.startXCoord,'ycoord':this.startYCoord + 2},
           {'xcoord':this.startXCoord,'ycoord':this.startYCoord+1},
         ]
       )
    break;

  default:console.log('something went wrong');
      break;
}
}



IShip(IshipCount){
  this.length = 3;
  var startXCoord =  this.getRandomInt(1,10);
  var startYCoord =  this.getRandomInt(1,10);
  var coords = new Array[startXCoord,startYCoord];
}
DotShip(DotShipCount){
  this.length = 1;
  var xCoord =  this.getRandomInt(1,10);
  var startYCoord =  this.getRandomInt(1,10);
  var coords = new Array[xCoord,startYCoord];
}







render(){
  return(
    <h1>{this.coords[0].xcoord}</h1>
  )
}
}

Your this reference is probably different in the place that you are setting and calling it. 您在设置和调用该引用时所使用的引用可能有所不同。

Either bind the function to this in the constructor like 在构造器中将函数绑定到此

this.LShip = this.LShip.bind(this);

Or maybe better yet store your coords in your state this.state and access it via this.state.coords[0].xcoord . 或者,也许最好将坐标保存在状态this.state ,并通过this.state.coords[0].xcoord访问它。 This has the added benefit that your component will re-render if these change. 这样做的好处是,如果这些内容发生更改,组件将重新呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM