is there any way to have redux-form ignore dots in a field name?

痞子三分冷 提交于 2019-12-24 07:47:11

问题


i have a restful api that accepts dotted(path) strings as parameter keys.

for example, i can call this:

/widgets?material.type=iron

so i have a redux-form field like:

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

but redux-form interprets the dot in material.type and when passed to my handler ends up as {material: {type: 'iron'}}.

i understand why this is the default behavior, but i was wondering if there is an easy way to thwart it with a property or something such that i can get {'material.type': 'iron'} instead?


回答1:


This is not possible. Redux Form will always nest field values under objects when their name contain a dot - internally _.toPath is used, and it doesn't have a way to escape characters.

What you could do instead is to rename the value when assigning initialValues:

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

...and when submitting the form:

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


来源:https://stackoverflow.com/questions/47536618/is-there-any-way-to-have-redux-form-ignore-dots-in-a-field-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!