简体   繁体   English

我需要帮助理解 React 应用程序中的 TypeError 吗?

[英]I need help understanding a TypeError in a React app?

I am trying to make a Kanban clone.我正在尝试制作看板克隆。 I have the following block of code which is giving a TypeError我有以下代码块,它给出了 TypeError

59 |  moveItemLeft(index1,index2) {
58 |     console.log(index1,index2)
60 |     let newArr = this.state.boards.slice();
61 |     let arrItem = newArr[index1].list[index2];
62 |     newArr[index1].list.splice(index2, 1);
63 |     newArr[(index1-1)].list.push(arrItem);
64 |     this.setState({
65 |           boards: newArr,
66 |   })

    }

The error I keep getting is我不断收到的错误是

TypeError: newArr[(index1 - 1)] is undefined
moveItemLeft
C:/Users/claude/documents/codecademy/premium/reactjs/triplebyte-trello/src/BoardContainer/BoardContainer.js:61

on line 61. I can't figure out why I am getting that error, the code should work.在第 61 行。我不明白为什么我会收到那个错误,代码应该可以工作。

Can anyone help?任何人都可以帮忙吗?

I am not getting clear scenario in your question without further lines of code.如果没有更多的代码行,我无法在您的问题中得到清晰的场景。 However, what I am understanding here is that the arrays are empty when line 61 is getting interpreted and it is getting value afterwards.但是,我在这里的理解是,当第 61 行被解释时数组是空的,然后它正在获取值。 please try placing line 61 after 63.请尝试将第 61 行放在 63 之后。

I solved the problem on line 63 I did the following:我解决了第 63 行的问题我做了以下事情:

if(index1 !== 0) newArr[(index1-1)].list.push(arrItem);

for whatever reason, the right arrow calls the moved left method which I will figure out.无论出于何种原因,右箭头都会调用向左移动的方法,我会弄清楚的。 So if index1 == 0, it cannot be decreased any farther.所以如果 index1 == 0,它就不能再减少了。 The same change will be made to the moveRight function, however, it will be if(index1 !== index1.length - 1).对 moveRight 函数进行相同的更改,但是,它将是 if(index1 !== index1.length - 1)。

Thank you everyone for your help.感谢大家的帮助。

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

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