简体   繁体   English

无法从函数打印字符串(在 react tictactoe 教程中)

[英]Can't print String from function (in react tictactoe tutorial)

I'm trying to implement the first improvement after completing the tictactoe react tutorial: Display the location for each move in the format (col, row) in the move history list.我正在尝试在完成 tictactoe react 教程后实现第一个改进: Display the location for each move in the format (col, row) in the move history list.

But my (col,row) 's, despite being correctly calculated, aren't being printed in the buttons.但是我的(col,row) 's 尽管计算正确,但没有打印在按钮中。 My buttons get texts like Go to move #1 (undefined) .我的按钮获得类似Go to move #1 (undefined)文本。 The (col,row) value should be printed instead of that undefined.应该打印(col,row)值而不是未定义的值。

Why is it happening?为什么会发生?

Here's my code:这是我的代码:

 render() {
  const history = this.state.history;
  const current = history[this.state.stepNumber];
  const winner = calculateWinner(current.squares);

  const moves = history.map((step,move) => {
    const desc = move ? 'Go to move # ' + move + ' ('+getRowCol(history, move)+ ')': 'Go to game start';
    return (
      <li key={move}><button onClick={() => this.jumpTo(move)}>{desc}</button></li>
    )
  });

 (...)

 function getRowCol(history, move){
  let array1 = history[move].squares;
  let array2 = history[move-1].squares;
  array1.forEach((item,index) => {
   if(item !== array2[index]){
    return nomearIndice(index);
   } 
  });
 }
 function nomearIndice(indice){
  let retorno = indice < 3 ? '1,' : indice < 6 ? '2,' : '3,';
  if([0,3,6].includes(indice)){
   return retorno + '1';
  } if([1,4,7].includes(indice)){ 
   return retorno + '2';
  } if([2,5,8].includes(indice)){
   return retorno + '3';
  }
 }

So I run history.map in line 6 of the code sample, this method calls getRowCol function and, as far as I can tell by putting a million console.log s in the code, it works fine.所以我在代码示例的第 6 行运行history.map ,该方法调用getRowCol函数,据我所知,通过在代码中放置一百万个console.log可以看出,它工作正常。 I guess JS isn't waiting until my getRowCol function returns and is producing this undesired result.我猜 JS 不会等到我的getRowCol函数返回并产生这个不想要的结果。 Is it the problem?这是问题吗? If so, how can I fix it?如果是这样,我该如何解决?

Thanks in advance提前致谢

It doesn't look like you are returning anything from getRowCol Try看起来你没有从 getRowCol 返回任何东西试试

function getRowCol(history, move) {
  let array1 = history[move].squares;
  let array2 = history[move - 1].squares;
  return array1.map((item, index) => {
    if (item !== array2[index]) {
      return nomearIndice(index);
    }
  });
}

Here is a demo using map and returning the result.是一个使用 map 并返回结果的演示。 However, I'm not sure what output you are expecting.但是,我不确定您期望什么输出。

Can you explain what result you want from getRowCol (eg an example) and I can try to help.你能解释一下你想要从 getRowCol 得到什么结果吗(例如一个例子),我可以尝试提供帮助。

I've updated the demo to reflect the behaviour you are after using我已更新演示以反映您使用后的行为

function getRowCol(history, move) {
  let array1 = history[move].squares;
  let array2 = history[move - 1].squares;

  return array1.reduce((accumulator, item, index) => {
    return (item === array2[index]) ? accumulator : nomearIndice(index);
  }, '');
}

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

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