简体   繁体   中英

How to supply an initial value to an individual Field in redux-form?

I am trying to build a form using FieldArrays from redux-form. Each field in the fields array needs to have an "order" field. I want to prepopulate this field with a default/initial value. I know how to supply initialValues to the form, but cannot figure out how to do it for an individual 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} />
      ...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM