简体   繁体   English

React-Native:无法将 state 中的数组大小设置为 state 中另一个数组的大小

[英]React-Native: Unable to set size of an array in the state to the size of another array in the state

I have an array in my state called purchaseItems, whose values are currently hardcoded.我的 state 中有一个名为 purchaseItems 的数组,其值当前是硬编码的。

I have another array named priceArray that I initialized to be empty in the state, and I've been trying to modify it in componentDidMount() so that it has the length of the purchaseItems array and is filled with zeroes.我有另一个名为 priceArray 的数组,我在 state 中初始化为空,我一直在尝试在 componentDidMount() 中对其进行修改,使其具有 purchaseItems 数组的长度并用零填充。

The way I've been trying to do is getting the length of purchaseItems, creating a duplicate of priceArray called newArray, using a for loop to push zeroes into newArray until it has the same length as purchaseItems, then appending newArray to priceArray via setState.我一直在尝试做的方法是获取 purchaseItems 的长度,创建名为 newArray 的 priceArray 的副本,使用 for 循环将零推入 newArray 直到它具有与 purchaseItems 相同的长度,然后通过 setState 将 newArray 附加到 priceArray。

However, priceArray remains empty when I check it via console.log.但是,当我通过 console.log 检查 priceArray 时,它仍然为空。 Why might this be?为什么会这样? I'll attach the code below.我将附上下面的代码。

Note: I'm planning to eventually populate purchaseItems from the DB, so the length of the purchaseItems array will not always be the same, so that's why I'm not hardcoding, say, an array full of zeroes with a length of 5 for priceArray if purchaseItems has a length of 5.注意:我计划最终从数据库中填充 purchaseItems,因此 purchaseItems 数组的长度并不总是相同,所以这就是我不硬编码的原因,例如,一个长度为 5 的充满零的数组priceArray 如果 purchaseItems 的长度为 5。

Code from my state:我的 state 中的代码:

  state = {};
  constructor(props) {
    super(props);
    this.state = {
      purchaseItems: [redacted],
      priceArray: [],
    };
  }

Code from my componentDidMount():我的 componentDidMount() 中的代码:

  componentDidMount() {
    console.log("Purchase Items: " + JSON.stringify(this.state.purchaseItems));
    const numItems = this.state.purchaseItems.length;
    console.log("numItems: " + numItems);
    var newArray = this.state.priceArray.slice();
    console.log("initialized newArray: " + JSON.stringify(newArray));
    var i;
    for (i = 0; i < numItems; i++) {
      newArray.push(0);
    }
    console.log("populated newArray: " + JSON.stringify(newArray));
    this.setState({ priceArray: [...this.state.priceArray, newArray] });
    console.log("priceArray: " + JSON.stringify(this.state.priceArray));
  }

Terminal:终端:

Purchase Items: [redacted]
numItems: 6
initialized newArray: []
populated newArray: [0,0,0,0,0,0]
priceArray: []

set state of priceArray properly正确设置 priceArray 的priceArray

this.setState({ priceArray: [...this.state.priceArray, ...newArray] },()=>console.log(his.state.priceArray));

You can also do the whole operation in one line您也可以在一行中完成整个操作

this.setState({priceArray:(new Array(this.state.purchaseItems.length)).fill(0))},()=>console.log(his.state.priceArray))

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

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