如何以 redux 形式为单个字段提供初始值?
How to supply an initial value to an individual Field in redux-form?
我正在尝试使用来自 redux-form 的 FieldArrays
构建一个表单。 fields
数组中的每个字段都需要有一个 "order" 字段。我想用 default/initial 值预填充此字段。我知道如何向表格提供 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} />
...
};
<Field meta={{initial: `${index + 1}}} />
https://redux-form.com/7.1.1/docs/api/field.md/#-code-meta-initial-any-code-
我正在尝试使用来自 redux-form 的 FieldArrays
构建一个表单。 fields
数组中的每个字段都需要有一个 "order" 字段。我想用 default/initial 值预填充此字段。我知道如何向表格提供 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} />
...
};
<Field meta={{initial: `${index + 1}}} />
https://redux-form.com/7.1.1/docs/api/field.md/#-code-meta-initial-any-code-