我正在尝试使用redux-form中的FieldArrays
构建表单。 fields
数组中的每个字段都需要有一个" order"领域。我想使用默认/初始值预填充此字段。我知道如何向表单提供initialValues
,但无法弄清楚如何为个人Field
执行此操作。
const renderQuestion = ({ fields, meta: { error, submitFailed } }) => (
<ul>
{fields.map((question, index) => (
<li key={index}>
<button type="button" title="Remove Question" onClick={() => fields.remove(index)}>
x
</button>
<h4>Question #{index + 1}</h4>
<Field
name={`${question}.order`}
value={index + 1} // This line is an example to what I am trying to achieve
type="number"
component={renderField}
label="Order" />
<Field name={`${question}.prompt`} type="text" component={renderField} label="Prompt" />
</li>
))}
</ul>
);
const QuizStepAddForm = props => {
...
<FieldArray name="questions" component={renderQuestion} />
...
};
答案 0 :(得分:0)
<Field meta={{initial: `${index + 1}}} />
https://redux-form.com/7.1.1/docs/api/field.md/#-code-meta-initial-any-code-