Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Redux表单未格式化post数据和[object]问题_Javascript_Reactjs_React Redux_Redux Form - Fatal编程技术网

Javascript Redux表单未格式化post数据和[object]问题

Javascript Redux表单未格式化post数据和[object]问题,javascript,reactjs,react-redux,redux-form,Javascript,Reactjs,React Redux,Redux Form,我有两个问题是彼此造成的。我用initialValue数据填充两个字段,然后可以将另一个字段推送到数组中。当我试图修改initialValue结构时,出现了以下问题: initialValues: { rockSingers: [ "Axl Rose", "Brian Johnson"] } 致: 第一个问题是该字段现在返回[object object]。提交表单后,将显示正确的json格式,直到我开始第二期。。。添加与初始值格式不同的新值时 { "rockSingers"

我有两个问题是彼此造成的。我用
initialValue
数据填充两个字段,然后可以将另一个字段推送到数组中。当我试图修改
initialValue
结构时,出现了以下问题:

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

第一个问题是该字段现在返回
[object object]
。提交表单后,将显示正确的
json
格式,直到我开始第二期。。。添加与
初始值格式不同的新值时

{
  "rockSingers": [
    {
      "singer": "Axl Rose"
    },
    {
      "singer": "Brian Johnson"
    },
    "Tom Rudge"
  ]
}
这是代码沙盒-

试试这个:

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>
    );
const renderRockSingers=({fields})=>(
摇滚歌手:
{fields.map((rockSinger,index)=>(
value.singer}
parse={value=>({singer:value})}
键={index}
component=“输入”
/>
))}
fields.push({singer:'})}>
添加更多
);

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

const renderRockSingers=({fields})=>(
摇滚歌手:
{fields.map((rockSinger)=>(
))}
fields.push()}>
添加更多
);
(值!==未定义的值。singer:)}
normalize={value=>({singer:value})}
/>

代码沙盒:

如果您没有使用任何嵌套的FieldArray,那么您可以使用数组格式指定初始值<代码>初始值:{rockSingers:[“Axl Rose”,“Brian Johnson”]}
其他事情都可以,因为这是一个很好的答案,我在我的场景中使用了这个选项,我还有其他事情要做。谢谢
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>
    );
<Field
  name={rockSinger}
  key={index}
  component="input"
  format={(value, name) => (value !== undefined ? value.singer : "")}
  normalize={value => ({ singer: value })}
/>