1

I have an array of objects and want to pass the data as initial data to the redux form's FieldArray component.

I looked through the docs for the FieldArray component but it is not clear about providing initial values.

Is there a way to accomplish this?

1 Answer 1

2

you will have to pass fieldArray filed as a array, you can achieve this by two ways:

1.pass a initialValues prop to reduxForm HOC config

const Form = reduxForm({
  form: 'simple' // a unique identifier for this form
  initialValues: {field1: "val1", fieldArrayFieldName: ["val1", "val2"]}
})(SimpleForm)

const SimpleForm = (props) => (
  ...
  <FieldArray name="fieldArrayFieldName" component={renderFieldArrayComp} />
  ...
)

const renderFieldArrayComp = ({ fields }) => (  
        {fields.map((field, index) => (
          <li key={index}>
            <Field
              name={hobby}
              type="text"
              component={renderField}
              label={`Hobby #${index + 1}`}
            />
          </li>
        ))}
      )

see docs for more

2.similarly you can also manually dispatch INITIALIZE action,

the reduxForm HOC will provide you a initialize action as a prop, you can use this in componentDidMount,

componentDidMount() => {
  const {initialize} = this.props;
  initialize({field1: "val1", fieldArrayFieldName: ["val1", "val2"]})
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.