简体   繁体   English

如何在ReactJs中使用扩展运算符将新值添加到数组的末尾

[英]How to add a new value to the end of an array using spread operator in ReactJs

I have initialized the state as: 我已经将状态初始化为:

this.state = {
    buildingNames: []
}

Then , I am adding new values to my array inside my function as : 然后,我将新值添加到函数内部的数组中,如下所示:

onClickAddBuildingTextBoxHandler = (inputData) => {
    this.setState({ buildingNames: [...this.state.buildingNames, inputData.value] });   
}

So, in my render function I am renderingthe values of the array as: 因此,在我的渲染函数中,我将数组的值渲染为:

this.state.buildingNames.map((buildings, i) => {
    return (
        <div key={'building' + i}>
            <div className={classes.sceBuildingNameContainer}>
                <div className={classes.sceBenchmarkingBuildingName}>
                    {this.state.buildingNames} //its returning a single element
                </div>
                <button
                   className={classes.sceCloseIcon}
                   onClick={this.onClickDeleteBuildingIconHandler}>
                       <i className={"fas fa-times " + classes.sceBuildingCloseIcon} />
               </button>
            </div>
        </div>
}

So, what is currently happening is, the map function is not working properly. 因此,当前正在发生的事情是,地图功能无法正常工作。 I mean the block is returning the same value twice. 我的意思是该块两次返回相同的值。 And when I add a new value, it gets appended to the previous state. 当我添加一个新值时,它会附加到以前的状态。 So, if my image , if I add a new value again,, it will get appended after mnpq .What I want is to add the new values one after the other.How do I set the new state for that case? 因此,如果我的图像再次添加新值,它将在mnpq之后附加。我想要的是一个接一个地添加新值。如何为这种情况设置新状态?

图片

You're using map to iterate over the elements in the buildings array. 您正在使用map来迭代buildings数组中的元素。 The first argument in the callback function is the current element, so change that from the plural buildings to building , and then reference that in your JSX {building} . 回调函数中的第一个参数是当前元素,因此将其从复数buildings更改为building ,然后在JSX {building}进行引用。 In addition there's no need to prefix "building" to the key id. 另外,无需在密钥ID前面加上“ building”。

this.state.buildingNames.map((building, i) => {
  return (
    <div key={i}>
      <div className={classes.sceBuildingNameContainer}>
        <div className={classes.sceBenchmarkingBuildingName}>
          {building}
        </div>
      </div>
    </div>
  );   
}

The first argument to the map function is a reference to the current element of the array map is looping on. map function的第一个参数是对循环中的数组map的当前元素的引用。

onClickAddBuildingTextBoxHandler = (inputData) => {
    const tempState = [...this.state.buildingNames];
    tempState.push(inputData.value);
    this.setState({ buildingNames: tempState });

  }



this.state.buildingNames.map((building, i) => {
    return (
        <div key={'building' + i}>
            <div className={classes.sceBuildingNameContainer}>
                <div className={classes.sceBenchmarkingBuildingName}>
                    {building} //Here you are refrencing the current element.
                </div>
                <button
                   className={classes.sceCloseIcon}
                   onClick={this.onClickDeleteBuildingIconHandler}>
                       <i className={"fas fa-times " + classes.sceBuildingCloseIcon} />
               </button>
            </div>
        </div>
}

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

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