【问题标题】:is there any way to have redux-form ignore dots in a field name?有没有办法让 redux-form 忽略字段名称中的点?
【发布时间】:2017-12-04 06:54:21
【问题描述】:

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

例如,我可以这样称呼:

/widgets?material.type=iron

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

<Field name="material.type" component={TextField} label="material type" />

但是 redux-form 会解释 material.type 中的点,当传递给我的处理程序时,最终会变成 {material: {type: 'iron'}}

我理解为什么这是默认行为,但我想知道是否有一种简单的方法可以通过属性或其他东西来阻止它,以便我可以得到{'material.type': 'iron'}

【问题讨论】:

  • 您找到解决此问题的方法了吗?

标签: redux-form


【解决方案1】:

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

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

initialValues: {
  ...formData,
  materialType: formData['material.type']
}

...提交表单时:

onSubmit (values) {
  return submitSomehow({
    ...values,
    'material.type': values.materialType
  })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    相关资源
    最近更新 更多