简体   繁体   English

在 React、Redux 应用程序中,我想使用选择器为对象数组中的每个对象计算小计(价格 * qtyCount)

[英]In a React,Redux app I want to calculate a subtotal (price * qtyCount) for each object in an array of objects using a selector

I edited my question and added the component in question.我编辑了我的问题并添加了相关组件。 I suspect maybe just me not understanding the syntax.我怀疑也许只是我不理解语法。

My current output in component looks like this 25.0018.00 it should be 25.00 for 1 object and 18.00 for next.我在组件中的当前输出看起来像这样25.0018.00 1 个对象应该是25.00 ,下一个对象应该是18.00

I have an array of objects that look like this:我有一组看起来像这样的对象:

Json:杰森:

counters: [
    {
      id: "lam1g8uzn",
      windowType: "First ",
      windowLocation: "Out",
      price: 5,
      qtyCount: 5,
      imgUrl:'long string'
    },
    {
      id: "r5hamynf3",
      windowType: "Second ",
      windowLocation: "In",
      price: 2,
      qtyCount: 9,
      imgUrl: 'long string'
    }
]

Here is my selector这是我的选择器

  const selectCounter = (state) => state.counter;

  export const selectCounterItems = createSelector(
   [selectCounter],
   (counter) => counter.counters,
  );

This is the selector in question这是有问题的选择器

  export const selectCounterSubtotal = createSelector(
      [selectCounterItems],
      (counters) => counters.map((counter) => (
      (counter.qtyCount * counter.price).toFixed(2)
    )),
  );

Here is my component where subtotal is displayed这是我的组件,其中显示小计

 import { connect } from 'react-redux';
 import { createStructuredSelector } from 'reselect';
 import { selectCounterSubtotal } from '../../redux/counter/counter- 
 selectors';

 const SubTotalDisplay = ({ subTotal }) => (
  // const subtotal = (qty * price).toFixed(2);
  <div className="subtotal-diaplay">
  <span> Subtotal $</span>
  <span className="subtotal">{subTotal}</span>
  </div>
 );

 const mapStateToProps = createStructuredSelector({
  subTotal: selectCounterSubtotal,
 });

export default connect(mapStateToProps, null)(SubTotalDisplay);

You're passing an array of strings to SubtotalDisplay .您将字符串数组传递给SubtotalDisplay When React gets an array node it renders each of the items in the array.当 React 获取数组节点时,它会渲染数组中的每个项目。 That's why you're getting "25.0018.00".这就是为什么你得到“25.0018.00”。

Consider updating the SubtotalDisplay component to render all subtotals passed to it:考虑更新SubtotalDisplay组件以呈现传递给它的所有小计:

const SubTotalDisplay = ({ subTotals }) => (
  subTotals.map((subTotal, index) => (
    <div className="subtotal-display" key={index}>
      <span> Subtotal $</span>
      <span className="subtotal">{subTotal}</span>
    </div>
  ))
);

const mapStateToProps = createStructuredSelector({
  subTotals: selectCounterSubtotal,
});

I found that my problem was actually in another utility function where I was mistakenly mutating data.我发现我的问题实际上是在另一个实用函数中,我错误地改变了数据。

//Right Way    
export const resetAllCounters = (counters) => counters.map((counter) => ({ 
       ...counter, qtyCount: 0 }));

// Wrong way mutating data
export const resetAllCounters = (counters) => counters.map((counter) => 
Object.assign(counter, { qtyCount: 0 })); 

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

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