简体   繁体   English

Javascript 和 React 设置数组中数组的状态并使用扩展运算符

[英]Javascript and React setting state of arrays within arrays and using spread operator

I using react and wanted to create an array of items and then use spread operator, but I'm having trouble setting it up.我使用 react 并想创建一个项目数组,然后使用扩展运算符,但是我在设置时遇到了麻烦。

const [mediaArray, setMediaArray] = useState(null);
const [newArray, setNewArray] = useState([]);

const handleNewSet = index => {
  setNewArray([...newArray[index], '']);
  mediaArray[index] = {
    ...mediaArray[index],
    ['sets']: [...newArray[index], ''],
  };
};

Getting the error: TypeError: Invalid attempt to spread non-iterable instance获取错误: TypeError:传播不可迭代实例的尝试无效

I have a button, that will add additional array of sets from newArray into the mediaArray .我有一个按钮,它将从newArray添加额外的集合newArraymediaArray But because I'm trying to iterate through the mediaArray , I want to make sure that the correct sets goes to the correct mediaArray.但是因为我试图遍历mediaArray ,所以我想确保正确的集合进入正确的 mediaArray。 I hope that makes sense我希望这是有道理的

So I'll eventually have 1 set of mediaArray with 1 set of sets所以我最终会有 1 套 mediaArray 和 1 套

mediaArray = {
  sets: ['data1', 'data2', ...] //this will be from a specific index of the newArray
}

Another mediaArray with another set of sets另一个带有另一组集合的 mediaArray

mediaArray = {
  sets: ['bb1', 'bb2', bb3, ...] //this will be from a specific index of the newArray
}

And so on...等等...

Eventually I would like to have newArray:最终我想要 newArray:

newArray = [['data1','data2', ...], [bb1, bb2, bb3, ...], [aa2, aa5, ...], [...]]

The first problem I am seeing is that you initialize your mediaArray with null : const [mediaArray, setMediaArray] = useState(null);我看到的第一个问题是你用null初始化你的mediaArrayconst [mediaArray, setMediaArray] = useState(null); but you are trying to access the index of it already in handleNewSet .但是您正在尝试访问它已经在handleNewSetindex You have not shared your full code, so I am assuming you are setting it to an empty array somewhere else, if not: make sure that you do!你还没有分享你的完整代码,所以我假设你将它设置为其他地方的空数组,如果没有:确保你这样做!

I am just guessing now that mediaArray[index] is undefined and spread it will throw the error that you cannot spread undefined.我现在只是猜测mediaArray[index]undefined并且传播它会抛出你不能传播未定义的错误。

Try the following code changes:尝试以下代码更改:

const [mediaArray, setMediaArray] = useState(null);
const [newArray, setNewArray] = useState([]);

const handleNewSet = index => {
  setNewArray([...newArray[index], '']);

  // clone array for state update
  const newMediaArray = [...mediaArray];

  // check if there is already this index in mediaArray
  if(mediaArray[index]) {
    // update index
    newMediaArray[index] = {
       ...mediaArray[index],
       ['sets']: [...newArray[index], ''],
    };
  } else {
   // create index in array
   newMediaArray[index] = {
       ['sets']: [...newArray[index], ''],
    };
  }
  // finally update state
  setMediaArray(newMediaArray);
};

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

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