【发布时间】:2020-06-25 10:32:14
【问题描述】:
我在 React 中使用 Formik,需要更新表单和 FieldArray 中的数据
<Formik
enableReinitialize={true} initialValues={{ title: '', somelocations: [{location: locationEntity.locations && locationEntity.locations.map(e => e.id)}] ...
<FieldArray
name="somelocations"
render={arrayHelpers => (
...
{values.locations && values.locations.length > 0 ? (
values.locations.map((location, index) => (
<div key={index}>
...
<div className="location-field-value form-group"><Field type="text" placeholder="https://some.com" required name={`somelocations.${index}.location`} /></span></div>
...
/>
因此,我从初始值中获取值,但它们的组织不正确。不是每个都在单独的输入中,而是全部用逗号分隔
1,2,3,4,5
如果我删除 'somelocation' 数组的属性 -> 位置,一切正常
{somelocations.${index}.location}
如何从数组属性的初始值正确组织对象数组?
【问题讨论】:
-
你能在codesandbox.io中添加一个可重现的例子吗?