Javascript 无法使用React js将数据推送并附加到状态

Javascript 无法使用React js将数据推送并附加到状态,javascript,reactjs,Javascript,Reactjs,这里我用数据来陈述 state = { Response: [ { "id": "15071", "name": "John", "salary": "53", "age": "23", "department": "admin" }, { "id": "15072", "name": "maxr", "salary": "5

这里我用数据来陈述

state = {
    Response: [
      {
        "id": "15071",
        "name": "John",
        "salary": "53",
        "age": "23",
        "department": "admin"
      },
      {
        "id": "15072",
        "name": "maxr",
        "salary": "53",
        "age": "23",
        "department": "admin"
      },
      {
        "id": "15073",
        "name": "Josef",
        "salary": "53",
        "age": "23",
        "department": "admin"
      },
      {
        "id": "15074",
        "name": "Ye",
        "salary": "53",
        "age": "23",
        "department": "admin"
      }
    ]
我正在表中显示这些记录。在表u中,会看到10条记录,并且在表顶部会有一个按钮,因此如果按下“附加”按钮,则每次按下按钮时都必须添加10条记录,数据必须相同,但必须使用以下逻辑进行附加。我试图通过按下10条记录来设置状态,如果我有1,2,3,4,5,6,7,8,9,10条记录,则尝试将其附加为ex如果我按append 1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10必须结束

appendEmployees() {
  var reLoadCount = 1;
  for (let i = 0; i < 10; i++) {
    const myObj = {
      id: 0,
      name: '',
      salary: 0,
      department: ''
    };
    myObj.id = +this.setState.employee[i].id + (reLoadCount * 10);
    myObj.name = this.setState.employee[i].name;
    myObj.salary = this.setState.employee[i].salary;
    myObj.department = this.setState.employee[i].department;
    this.setState.employee.push(myObj);
  }
  reLoadCount++;
}
appendEmployees(){
var-reLoadCount=1;
for(设i=0;i<10;i++){
常数myObj={
id:0,
名称:“”,
工资:0,
部门:''
};
myObj.id=+this.setState.employee[i].id+(重载计数*10);
myObj.name=this.setState.employee[i].name;
myObj.salary=this.setState.employee[i].salary;
myObj.department=this.setState.employee[i].部门;
this.setState.employee.push(myObj);
}
重载计数++;
}

我在这里做错了什么吗

如果我做对了,您试图在
this.state.employee
数组中添加10个重复的对象,这些新对象和现有对象之间的唯一区别是它们的
id

如果是这种情况,以下是您可以做到的:

appendEmployees() {
  this.setState(prevState => {
    // Get the biggest ID number.
    const maxId = Math.max(...prevState.employee.map(e => parseInt(e.id)));
    // create 10 new employees copies of the first 10.
    const newEmployees = prevState.employee.slice(0, 10).map((e, i) => ({
      ...e,
      id: (maxId + i + 1)
    }));
    // return/update the state with a new array for "employee" that is a concatenation of the old array and the array of the 10 new ones.
    return {
      employee: [...prevState.employee, ...newEmployees]
    }
  });
}
我在示例中添加了一些注释来解释它的作用


重要的是
this.setState
,它是用于更新状态的函数,这里,我将它与一个函数一起用作第一个参数(它也适用于对象),我这样做是因为它是从旧状态生成新状态的首选方法。

您的问题不明确。你想要实现什么?试着把问题分解,使之合乎逻辑。目前询问的方式非常混乱。
myObj.department=this..setState.employee[i].department
@Eddie更新问题请检查double
是否仍在线
myObj.department=this..setState.employee[i].department
这本质上是您需要了解的React API中最重要、最基本的部分,因此我建议您搁置正在做的事情,首先通过React网站上最优秀的教程学习“如何进行React”。这需要一个小时,两个小时,如果你是React新手,或者你使用过React的旧版本,需要复习。好了,现在更新新记录也需要一个post请求,所以我必须将员工作为一个整体发送?或者任何其他事情?@Dexter如果您将函数作为第二个参数传递给
setState
,它将在设置新状态时执行,并将新状态作为参数,例如:
this.setState(prevState=>{},newState=>{/*访问新状态,进行API调用*/})
收到了,我必须发送this.state.employee,因为我会将所有更新的记录发送到帖子正文,然后就可以了