[英]How to add two fields in a form and show in 3rd field using Redux Form
I am using Redux-Form, where I have 2 fields 'client income' and 'partner income', and I need to show the sum of the values in another field, onChange of above 2 fields. 我正在使用Redux-Form,其中有2个字段“客户收入”和“合作伙伴收入”,我需要在另一个字段onChange上面的2个字段中显示值的总和。
I tried storing the sum in a global variable but it's not working. 我尝试将总和存储在全局变量中,但无法正常工作。
The behavior is like below, if I try adding 12+ 13 instead of 25, it's coming as 27. I identified the cause, it's happening as it is taking it as 1+12+1+13. 行为如下所示,如果我尝试添加12+ 13而不是25,则结果为27。我确定了原因,它的发生是因为将其视为1 + 12 + 1 + 13。 I am not sure how to mitigate the issue. 我不确定如何缓解此问题。
I could have used onBlur , but onChange is the requirement. 我本可以使用onBlur ,但必须使用onChange 。
Any help would be appreciated. 任何帮助,将不胜感激。
Can you use debounce? 可以使用防抖功能吗? There's an NPM module react-debounce-input to give you a general idea of implementation. 有一个NPM模块react-debounce-input可以为您提供一般的实现思路。
Use formValueSelector
to connect() to form values and these values will be available in props of your form. 使用formValueSelector
来connect()来形成表单值,这些值将在表单的props中可用。
const selector = formValueSelector('simple');
SimpleForm = connect(
state => {
return {
number1: selector(state, 'number1'),
number2: selector(state, 'number2'),
}
}
)(SimpleForm);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.