reactjsreact-final-formreact-final-form-arrays

how to show initial values for FieldArray


I have ann app that users go and add as many text-fields as they want and enter their cars' names (I did it easily via FieldArray).

Now the problem is I need an Edit page where users go and see all the cars that they had entered in add page and be able to edit them. I need to render the FieldArray again and use something like InitialValues.

So if the user created, for instance, 5 cars in the Add page, when he/she goes to the Edit page I want to see 5 inputs there created via FieldArray with the cars' names. How to do that?


Solution

  • You'd need to initialize the form via initialValues with the array of car names. Something like...

    // load values from server
    const values = {
      cars: [
        'Alex', 'Bob', 'Carol', 'Dennis', 'Erik'
      ]
    }
    
    ...
    
    <Form onSubmit={whatever} initialValues={values}>
      {({ handleSubmit }) => (
        <form onSubmit={handleSubmit}>
          // fields here, just like you did on "add" page
        </form>
      )}
    </Form>