Reactjs 从返回NAN而不是number的选择器添加值
我的总回报率很低。我试图让它在下面的代码段中添加所有可用的选择器。见总行Reactjs 从返回NAN而不是number的选择器添加值,reactjs,ecmascript-6,redux,redux-form,Reactjs,Ecmascript 6,Redux,Redux Form,我的总回报率很低。我试图让它在下面的代码段中添加所有可用的选择器。见总行 Form = connect(state => { const equipmentTotal = selector(state, 'equipment.total'); const softwareTotal = selector(state, 'softwares.total'); const thirdPartyEquipmentTotal = selector(state, 'thirdParty
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。最简单的方法是使用| |或JavaScript操作符。它返回第一个真实值。因此,如果选择器返回的值未定义、为null,甚至为0,它将返回0
这是可行的,但会产生22+22=022022。。。这似乎仍然是一个与字符串相关的问题。redux表单字段都设置为type=number,但在redux状态下,它们显示为22。我应该用Numberval之类的东西添加一个规范化器吗?我被这个弄糊涂了…嗯,是的,我也很困惑。您应该确保存储在reducer中的值存储为数字而不是字符串。在将它们保存到减速器状态之前,您可能想尝试PARSETIN。您还可以这样做:Toe:PosiTimeQuePoToTrand,10 + PARSETIN软件包,10 + PARSETIN PARDPARTION设备总计,10…,但这只是在实际问题上提供了一个创可贴。
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);