如何动态地将对象添加到 react-final-form 字段名称?

问题描述

这个 react-final-form 组件会返回如下结果

<Field
   name="answers[0].name"
   component="input"
   type="radio"
   value="0"
/>


{ answers: [ { name: 'value' } ] } 

如何添加一个属性?会这样结束

{ answers: [ { name: 'user input value will here',other: 'my custom data' } ] } 

根据最终形式,您可以通过 4 种方式命名您的字段组件

enter image description here

解决方法

您可以向表单添加一个 initialValues 道具,该道具将预填充结果。

<Form
    onSubmit={onSubmit}
    initialValues={{
        answers: [ { other: 'your custom data' } ]
    }}
>
    <Field
        name="answers[0].name"
        component="input"
        type="text"
    />
</Form>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...