Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在地图中为我的钥匙道具生成唯一的钥匙?反应_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在地图中为我的钥匙道具生成唯一的钥匙?反应

Javascript 如何在地图中为我的钥匙道具生成唯一的钥匙?反应,javascript,reactjs,Javascript,Reactjs,我有以下代码: class Board extends Component { static defaultProps = { nrows: 5, ncols: 5, chanceLightStartsOn: 0.25 }; constructor(props) { super(props); this.state = { hasWon: false, board: this.createBoard(),

我有以下代码:


class Board extends Component {
  static defaultProps = {
    nrows: 5,
    ncols: 5,
    chanceLightStartsOn: 0.25
  };

  constructor(props) {
    super(props);
    this.state = {
      hasWon: false,
      board: this.createBoard(),
      counter: 0,

    };

      this.createBoard = this.createBoard.bind(this);
      this.keyCount = this.keyCount.bind(this);
  }
[...]
   render() {

    const mainBoard = Array.from({length: this.props.nrows}).map(() => (
        <tr>
          {Array.from({length: this.props.ncols}).map((x, index) => (
            <Cell key={this.state.counter + 1} onClick={() => this.flipCellsAround()} isLit={this.props.chanceLightStartsOn > Math.random()} />
            ))}
        </tr>
      ));

    return (
      <table className="Board">
        <tbody>
          <h1>BOARD</h1>
          {mainBoard}
        </tbody>
      </table>

    );

类板扩展组件{
静态defaultProps={
nrows:5,
ncols:5,
Chancellightstartson:0.25
};
建造师(道具){
超级(道具);
此.state={
哈斯旺:错,
board:this.createBoard(),
柜台:0,,
};
this.createBoard=this.createBoard.bind(this);
this.keyCount=this.keyCount.bind(this);
}
[...]
render(){
const mainBoard=Array.from({length:this.props.nrows}).map(()=>(
{Array.from({length:this.props.ncols}).map((x,index)=>(
this.flipCellsAround()}isLit={this.props.ChancellStartson>Math.random()}/>
))}
));
返回(
董事会
{主板}
);

我希望在每次迭代中,我的
键都会增加1,因此它是唯一的。我尝试了很多事情,但都没有成功。是否可以将函数传递给
键,然后在每次迭代中增加1?

您只需将
index
变量传递给key prop并将其组合起来即可如果不调整表的大小,请使用外部
map()
中的索引。理想情况下,它应该是一个与列内容具有某种逻辑连接的数字,以便react知道在
单元格
组件发生更改时重新呈现列

const mainBoard = Array.from({length: this.props.nrows}).map((ignored, rowIndex) => (
    <tr>
      {Array.from({length: this.props.ncols}).map((x, idx) => (
        <Cell key={rowIndex * this.props.nrows + idx} onClick={() => this.flipCellsAround()} isLit={this.props.chanceLightStartsOn > Math.random()} />
        ))}
    </tr>
  ));


有一个名为
uuid
的npm模块,在这里检查它,这不起作用吗?key={index}@JonWilson不,我试过它只是给出了这样的结果:
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
,如果这样做有意义的话?或者它可以是完全随机的,正如@blacksheep在这个问题上所建议的那样,我需要所有的键都是唯一的。如果我使用
索引
当我检查我的表格时,我看到的键是:
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
0,1,2,3,4
,如果这样做有意义的话?@AngelaInniss尝试一下这个版本。这实际上非常有效,这正是我想要的-谢谢你和我的说明。