2

Was wondering if Formik has a native solution for identifying the addition and deletion (and update) of FieldArray in the form ?

I have the code on sandbox here https://codesandbox.io/s/jn7x2m75o9 ( based on the original Formik Array example @ https://github.com/jaredpalmer/formik/blob/master/examples/Arrays.js )

but also the relevant part here :

With an Initial state of 3 friend defined, how can I know in my onSubmithandler which one were modified,deleted,updated.

import React from "react";
import { Formik, Field, Form, ErrorMessage, FieldArray } from "formik";

const initialValues = {
  friends: [
    {
      name: "Friend_A",
      email: "[email protected]"
    },
    {
      name: "Friend_B",
      email: "[email protected]"
    },
    {
      name: "Friend_C",
      email: "[email protected]"
    }
  ]
};

const mySubmit = values => console.log();

const SignIn = () => (
  <div>
    <h1>Invite friends</h1>
    <Formik
      initialValues={initialValues}
      onSubmit={values => {
        var itemRemoved = values.GetItemRemoveFromArray; // This is what I'm looking for
        console.log(itemRemoved);
        // Would print Friend_A

        var itemAdded = values.GetItemAddedFromArray; // This is what I'm looking for
        console.log(itemAdded);
        // Would print New_Friend

        var itemUpdated = values.GetItemUpdatedInArray; // This is what I'm looking for
        console.log(itemUpdated);
        // Would print Friend_C

        setTimeout(() => {
          alert(JSON.stringify(values, null, 2));
        }, 500);
      }}
      render={({ values }) => (
        <Form>
          <FieldArray
            name="friends"
            render={({ insert, remove, push }) => (
              <div>
                {values.friends.length > 0 &&
                  values.friends.map((friend, index) => (
                    <div className="row" key={index}>
                      <div className="col">
                        <label htmlFor={`friends.${index}.name`}>Name</label>
                        <Field
                          name={`friends.${index}.name`}
                          placeholder="Jane Doe"
                          type="text"
                        />
                        <ErrorMessage
                          name={`friends.${index}.name`}
                          component="div"
                          className="field-error"
                        />
                      </div>
                      <div className="col">
                        <label htmlFor={`friends.${index}.email`}>Email</label>
                        <Field
                          name={`friends.${index}.email`}
                          placeholder="[email protected]"
                          type="email"
                        />
                        <ErrorMessage
                          name={`friends.${index}.name`}
                          component="div"
                          className="field-error"
                        />
                      </div>
                      <div className="col">
                        <button
                          type="button"
                          className="secondary"
                          onClick={() => remove(index)}
                        >
                          X
                        </button>
                      </div>
                    </div>
                  ))}
                <button
                  type="button"
                  className="secondary"
                  onClick={() => push({ name: "", email: "" })}
                >
                  Add Friend
                </button>
              </div>
            )}
          />
          <button type="submit">Invite</button>
        </Form>
      )}
    />
  </div>
);

export default SignIn;

So if with the above a user where to :

  1. Click on the X below Friend_A
  2. Modify Friend_C email to [email protected]
  3. Click “Add Friend”
  4. Enter value Name: New_Friend_X and email: [email protected]
  5. Click “Add Friend”
  6. Enter value Name: New_Friend_Z and email: [email protected]
  7. Click “X” button below newly entered “New_Friend_X”
  8. Click “Invite”

in my mySubmit I’m looking for a way to easily get :

  1. Friend_A was Removed
  2. Friend_C was Modified
  3. New_Friend_Z was added (was not in the original initialValues to formik)

(I Don’t care about New_Friend_X. No need to know it was added/removed )

Point of this is to minimize rest call to the back end to create/update entity/link and also I really dont want to write my own “secondary state” in the onClick handler of the remove button before calling the remove(index) handler provided by Formik to track what need to be deleted from the DB.