有没有办法让 redux-form 忽略字段名称中的点?

ton*_*y_k 2 redux-form

我有一个安静的 api,它接受点(路径)字符串作为参数键。

例如,我可以称之为:

/widgets?material.type=iron
Run Code Online (Sandbox Code Playgroud)

所以我有一个 redux-form 字段,如:

<Field name="material.type" component={TextField} label="material type" />
Run Code Online (Sandbox Code Playgroud)

但是 redux-form 将点解释material.type{material: {type: 'iron'}}.

我明白为什么这是默认行为,但我想知道是否有一种简单的方法可以用属性或其他东西来阻止它,我可以{'material.type': 'iron'}代替它?

gus*_*nke 5

这不可能。当对象的名称包含点时,Redux 表单将始终在对象下嵌套字段值 - 在内部_.toPath使用,并且它没有办法转义字符。

您可以做的是在分配时重命名该值initialValues

initialValues: {
  ...formData,
  materialType: formData['material.type']
}
Run Code Online (Sandbox Code Playgroud)

...当提交表单时:

onSubmit (values) {
  return submitSomehow({
    ...values,
    'material.type': values.materialType
  })
}
Run Code Online (Sandbox Code Playgroud)