如何以redux形式为单个Field提供初始值?

时间:2017-10-10 17:18:56

标签: reactjs redux redux-form

我正在尝试使用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} />
      ...
};

1 个答案:

答案 0 :(得分:0)

相关问题