SaidAkh
SaidAkh

Reputation: 1861

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} />
      ...
};

Upvotes: 1

Views: 1642

Answers (1)

kngroo
kngroo

Reputation: 462

<Field meta={{initial: `${index + 1}}} />

https://redux-form.com/7.1.1/docs/api/field.md/#-code-meta-initial-any-code-

Upvotes: 1

Related Questions