简体   繁体   English

reactjs 中的多维 state 更新

[英]Multidimensional state update in reactjs

I am working with a complex state object in reactjs and I am unable to update a particular item of the state.我正在使用 reactjs 中的复杂 state object 并且我无法更新 Z9ED39E28EA9314586B6 的特定项目

state = {
    order: {
      jobs: [
        {
          id: "1",
          jobId: "31147-02",
          services: [
            {
              id: 18,
              price: "100",
            },
            {
              id: 19,
              price: "100",
            },
            {
              id: 65,
              price: "3200",
            },
            {
              id: 87,
              price: "1800",
            },
            {
              id: 88,
              price: "1350",
            },
            {
              id: 89,
              price: "1350",
            },
          ],
        },
        {
          id: "2",
          jobId: "31147-02",
          services: [
            {
              id: 45,
              price: ""
            },
            {
              id: 41,
              price: "",
            },
            {
              id: 28,
              price: "",
            },
          ],
        },
      ],
    },
  };

And trying to handle delete functionality并尝试处理删除功能

Following code of is receiving the index of JOB and the object of service which needs to be removed from the state to perform delete functionality.以下代码是接收 JOB 的索引和需要从 state 中删除的服务的 object 以执行删除功能。

handleJobServiceDelete = (jobId, service) => {
    let jobs = [...this.state.order.jobs[jobId].services];
    jobs = jobs.filter((s) => s.id !== service.id);
    console.log(jobs);
    this.setState({ jobs });
};

But I am unable to setState with updated service array after removing object from it.但是在从中删除 object 后,我无法使用更新的服务数组设置状态。

You'd need to use this.setState({ order: { jobs } });您需要使用this.setState({ order: { jobs } }); , but that wouldn't work because React won't set the state fully, so it takes a bit more. , 但这行不通,因为 React 不会完全设置 state,因此需要更多时间。

this.setState((prevState)=>({ order: {...prevState.order, jobs } }));

The way I would ordinarily manage deeply nested state like this is using https://github.com/kolodny/immutability-helper as it makes it much easier to manage.我通常会像这样管理深度嵌套的 state 的方式是使用https://github.com/kolodny/immutability-helper ,因为它更容易管理。

Edit: Fixed the full issue using immutability helper.编辑:使用不变性助手修复了完整的问题。 There were different issues than I originally noticed.存在与我最初注意到的不同的问题。 I didn't realize due to the variable names that the data being altered was the job's services and so had to go back into the job's services rather than replacing the jobs.由于变量名,我没有意识到被更改的数据是作业的服务,因此必须将 go 重新放入作业的服务中,而不是替换作业。

import update from 'immutability-helper';

this.setState((prevState) => {
  const idx = prevState.order.jobs[jobId].services.findIndex(
    (s) => service.id === s.id
  );
  return {
    order: update(prevState.order, {
      jobs: { [jobId]: { services: { $splice: [[idx, 1]] } } },
    }),
  };
});

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

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