首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从选择器中添加值,返回NAN而不是数字

从选择器中添加值,返回NAN而不是数字
EN

Stack Overflow用户
提问于 2018-03-27 18:09:14
回答 1查看 466关注 0票数 0

我总能得到南的回报。我试图让它在下面的代码中添加所有可用的选择器。看整条线。

代码语言:javascript
复制
Form = connect(state => {
  const equipmentTotal = selector(state, 'equipment.total'); 
  const softwareTotal = selector(state, 'softwares.total');
  const thirdPartyEquipmentTotal = selector(state, 'thirdPartys.totalFees');
  const miscTotal = selector(state, 'miscTotal');
  const tradeInTotal = selector(state, 'orderHeader.tradein');

  return { //Returning data like the following is the equivalent of using ({ }) which is an object-literal as an expression in ES6... docs for more info.
    total: equipmentTotal + softwareTotal + thirdPartyEquipmentTotal + miscTotal + tradeInTotal,
  };
  },
)(Form);

但是,如果单个选择器未定义(null),则返回NAN。如果没有在状态中定义数字,是否可以将选择器设置为返回零?理想情况下,我不希望在表单字段中设置默认值0,因为它需要用户突出显示和删除它,或者可能意外地将0添加到输入数字的末尾。因此,用户在选择字段yeilding 290后不小心键入29。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-27 18:23:47

最简单的方法是使用||(或) JavaScript运算符。它返回第一个真实值。因此,如果从选择器返回的值是undefindednull,甚至是0,它将返回0

代码语言:javascript
复制
Form = connect(state => {
    const equipmentTotal = selector(state, 'equipment.total') || 0;
    const softwareTotal = selector(state, 'softwares.total') || 0;
    const thirdPartyEquipmentTotal = selector(state, 'thirdPartys.totalFees') || 0;
    const miscTotal = selector(state, 'miscTotal') || 0;
    const tradeInTotal = selector(state, 'orderHeader.tradein') || 0;

    return { //Returning data like the following is the equivalent of using ({ }) which is an object-literal as an expression in ES6... docs for more info.
        total: equipmentTotal + softwareTotal + thirdPartyEquipmentTotal + miscTotal + tradeInTotal,
    };
},
)(Form);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49519735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档