formik-persist-remote-values

Persist and rehydrate a Formik values

Usage no npm install needed!

<script type="module">
  import formikPersistRemoteValues from 'https://cdn.skypack.dev/formik-persist-remote-values';
</script>

README

Formik Persist Values

Persist and rehydrate a Formik form values.

npm install formik-persist-remote-values --save
yard add formik-persist-remote-values

Basic Usage

Just import the <PersistFormikValues > component and put it inside any Formik form. It renders null!

import React from 'react';
import { Formik, Field, Form } from 'formik';
import { PersistFormikValues } from 'formik-persist-values';

export const Signup = () => (
  <div>
    <h1>My Cool Persisted Values</h1>
    <Formik
      onSubmit={values => console.log(values)}
      initialValues={{ firstName: '', lastName: '', email: '' }}
    >
      {props => (
        <Form className="whatever">
          <Field name="firstName" placeholder="First Name" />
          <Field name="lastName" placeholder="Last Name" />
          <Field name="email" type="email" placeholder="Email Address" />
          <button type="submit">Submit</button>
          <PersistFormikValues name="signup-form" />
        </Form>
      )}
    </Formik>
  </div>
);

Props

  • name: string: LocalStorage key to save form state to
  • ignoreValues:? string[]: Provide array of keys if you need to do not persist some values
  • debounce:? number: Default is 300. Number of ms to debounce the function that saves form state.
  • storage:? 'localStorage' | 'sessionStorage' | Storage | CustomStorage: default is localStorage . Send if you want Session storage or your own storage instead of Local storage
  • persistInvalid:? boolean: default is false . Persist if you want to save invalid values
  • hashInitials:? boolean: default is false . Hash initials values to prevent conflict between initialValues and persistedValues.
  • hashSpecificity:? number: default is 7 . Hash initials values specificity to prevent conflict between cache hashes.

Author

  • Alexsander Rocha [@alex-amaral]

Inspired by