Redux-form 未格式化 post 数据和 [object Object] 问题

Redux-form not formatting post data and [object Object] issue

我有两个互为结果的问题。我用 initialValue 数据填充两个字段,然后我可以将另一个字段推送到数组。当我尝试修改 initialValue 结构时出现了这个问题:

  initialValues: {
    rockSingers: [ "Axl Rose", "Brian Johnson"]
  }

至:

  initialValues: {
    rockSingers: [{ singer: "Axl Rose" }, { singer: "Brian Johnson" }]
  }

第一个问题是字段现在returns[object Object]。提交表格后,会显示正确的 json 格式,直到我开始我的第二期...当添加一个与 initialValue 数据格式不同的新值时 - 例如

{
  "rockSingers": [
    {
      "singer": "Axl Rose"
    },
    {
      "singer": "Brian Johnson"
    },
    "Tom Rudge"
  ]
}

这是codesandbox - https://codesandbox.io/s/8kzw0pw408

试试这个:

const renderRockSingers = ({ fields }) => (
      <div>
        <h3>Rock Singers:</h3>
        {fields.map((rockSinger, index) => (
          <div>
            <Field
              name={rockSinger}
              format={value => value.singer}
              parse={value => ({ singer: value })}
              key={index}
              component="input"
            />
          </div>
        ))}
        <button type="button" onClick={() => fields.push({ singer: '' })}>
          Add more
        </button>
      </div>
    );

修改 renderRockSingers 以便抓取对象,而不是字符串。

const renderRockSingers = ({ fields }) => (
  <div>
    <h3>Rock Singers:</h3>
    {fields.map((rockSinger) => (
      <div>
        <Field name={`${rockSinger}.singer`} key="index" component="input" />
      </div>
    ))}
    <button type="button" onClick={() => fields.push()}>
      Add more
    </button>
  </div>
);

更多关于 FieldArray 组件的信息:fieldarrays

<Field
  name={rockSinger}
  key={index}
  component="input"
  format={(value, name) => (value !== undefined ? value.singer : "")}
  normalize={value => ({ singer: value })}
/>

代码沙箱:https://codesandbox.io/s/7m1p9600y0