Reactjs 添加redux表单字段数组的值

Reactjs 添加redux表单字段数组的值,reactjs,redux,redux-form,Reactjs,Redux,Redux Form,对于FieldArray中的每个新项,我希望将FieldArray中名为total的字段的内容添加到一起 在表单状态下,类似于这种数据结构 test.0.total + test.1.total + test.2.total = testTotal 我不确定访问表单中数据的选项是什么。如果我特别列出了期望的字段,formValueSelector工作得很好,但是考虑到这是一个FieldArray,我不知道在任何给定的时间会有多少项。如何循环数组结构以添加这些字段 我目前正在使用一个类似这样的选

对于FieldArray中的每个新项,我希望将FieldArray中名为total的字段的内容添加到一起

在表单状态下,类似于这种数据结构

test.0.total + test.1.total + test.2.total = testTotal
我不确定访问表单中数据的选项是什么。如果我特别列出了期望的字段,formValueSelector工作得很好,但是考虑到这是一个FieldArray,我不知道在任何给定的时间会有多少项。如何循环数组结构以添加这些字段

我目前正在使用一个类似这样的选择器来返回数组

const selector = formValueSelector('mainForm');
      export const mapState = (state, ownProps) => {
        const Total = selector(state, 'test');
        return { Total };
      }
我如何在选择器中潜在地构建循环机制

像这样的东西工作…,但不是动态的

    const selector = formValueSelector('mainForm');
      export const mapState = (state, ownProps) => {
        const test1 = selector(state, 'test.0.total');
        const test2 = selector(state, 'test.1.total');
        const test3 = selector(state, 'test.2.total');
       return { test1, test2, test3, 
        total: test1 + test2 + test3 };
      }
添加这样的内容

const test = selector(state, "test");
const sum = test.reduce((total, item) => total + Number(item.total), 0);
甚至是这个作为回报

sum: test.reduce((total, item) => total + Number(item.total), 0);
yeilds a TypeError:无法读取未定义的属性“reduce”。。。即使我能看见

我可以看到数组在状态中不是未定义的。。。有办法解决这个问题吗

arr.reduce
是您需要的


如果您的数据结构不包含带有
item.total
的对象,则将该部分替换为
item

关于如何与redux表单集成的想法?添加了用例。我试图将其保持最小以防止TLDR。您需要从状态获取数组并将其缩减,就像我在示例中所做的那样,并返回值。因此,这看起来可以工作,但返回TypeError:无法读取未定义的属性“reduce”。我在这方面有什么遗漏吗?数据结构是否传递了一个数组,它是否为空?在减少
arr和&arr.reduce“剩余代码…”之前添加此逻辑
const sum = arr.reduce((total, item) => total + Number(item.total), 0);