简体   繁体   English

如何在 map ZC1C425268E68385D1AB5074C17A94F1 中更改 object state

[英]How to change a object state inside a map function

I need to increase or decrease state value in catalog > spec > units , if I click on increase button the number in units should increase by one and if I click on decrease button it should decrease by one, I'd tried by setting state in the render, but it didn't work and I think this is not a good practice.我需要在catalog > spec > units中增加或减少 state 值,如果我点击增加按钮, units数应该增加一,如果我点击减少按钮,它应该减少一,我尝试通过在中设置 state渲染,但它不起作用,我认为这不是一个好习惯。 How can I create a function to setState of units without declaring it inside the render method?如何创建一个 function 来设置units的状态,而不在渲染方法中声明它?

Here is an example of my code:这是我的代码示例:

export default class Order extends Component {

  constructor(props) {
    super(props);
    this.state = {
      catalog: [
        {
          photo: 'https://via.placeholder.com/400x400',
          title: 'My title',
          description: 'Bla bla bla...',
          spec: { size: 'FAM', units: 1, price: 999999, id: 'CMB0', selectedIndicator: '', isSelected: false, name: 'A simple name' },
          isCombo: true
        },
      ],
    }
  }
}

render(){
   return(
      {this.state.catalog.map((item, index) => {
         <div key={index}>
            <strong>{item.title}</strong>
            <span>{item.spec.units}</span>
            <button onClick={() => item.spec.units + 1}>increase</button>
            <button onClick={() => item.spec.units - 1}>decrease</button>
         </div>})
      }
   )
}

Try this尝试这个

increase = title => {
    const newCatalogState = this.state.catalog.map(item => {
      if (item.title === title) {
        return {
          ...item,
          spec: {
            ...item.spec,
            units: item.spec.units + 1
          }
        };
      }
      return item;
    });
    this.setState({
      catalog: newCatalogState
    });
  };

 decrease = title => {
    const newCatalogState = this.state.catalog.map(item => {
      if (item.title === title) {
        return {
          ...item,
          spec: {
            ...item.spec,
            units: item.spec.units - 1
          }
        };
      }
      return item;
    });
    this.setState({
      catalog: newCatalogState
    });
  };

<button onClick={() => this.increase(item.title)}>increase</button>
<button onClick={() => this.decrease(item.title)}>decrease</button>

you can check here codesandbox hope it helps你可以在这里检查代码和框希望它有帮助

Try this:尝试这个:


export default class Order extends Component {

  constructor(props) {
    super(props);
    this.state = {
      catalog: [
        {
          photo: 'https://via.placeholder.com/400x400',
          title: 'My title',
          description: 'Bla bla bla...',
          spec: { size: 'FAM', units: 1, price: 999999, id: 'CMB0', selectedIndicator: '', isSelected: false, name: 'A simple name' },
          isCombo: true
        },
      ],
    }
  }
}

const updateUnits = (index, value) => {
  const { catalog } = this.state
  catalog[index].spec.units += value
  
  this.setState({catalog})
}

render(){
   return(
      { this.state.catalog.map((item, index) => {
         <div key={index}>
            <strong>{item.title}</strong>
            <span>{item.spec.units}</span>
            <button onClick={() => this.updateUnits(index, 1)}>increase</button>
            <button onClick={() => this.updateUnits(index, -1)}>decrease</button>
         </div>})
      }
   )
}

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

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