简体   繁体   English

如何使用Redux Form在表单中添加两个字段并在第3个字段中显示

[英]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);

Codesandbox demo Codesandbox演示

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

相关问题 如何将两个数字相加并将其与第三个数字相乘,并在 html 表单中以 javascript 的形式在禁用输入中显示总数? - How do I add two numbers and them multiply it with the 3rd number and show the total in a disabled input in a html form with javascript? Kendo UI:如何对两个字段求和并在第三个字段中显示 - Kendo UI: How to SUM two fields and show in a 3rd one 表单验证适用于 2 个字段,但不适用于第三个字段 - 什么破坏了验证检查 - Form Verification works on 2 fields but not the 3rd field - whats breaking the validation check JQuery - 在第 3 方表单上预填充表单字段 - JQuery - prepopulate form field on 3rd party form 如何将文本从单个表单域传递到两个表单域 - How to pass text from a single form field to two form fields 表单提交字段为空白。 使用Javascript隐藏和显示字段 - Form submitting field as Blank. Using Javascript to hide and show fields 如何使用angularjs添加其他表单字段? - How to add additional form fields using angularjs? Redux表单简单字段 - Redux Form Simple Fields 如何使用Redux表单预填充输入字段 - How to pre-populate input field using Redux form 如何使用Redux形式以编程方式更新字段的状态 - How to update the state of a field programmatically using redux-form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM