cofi

cofi

  • Docs
  • Form Demos
  • Components Demos
  • Layout Demos
  • Editor
  • GitHub

›Advanced Usages

Getting started

  • Introduction

Form Class

  • Installation
  • Overview
  • Arguments
  • Id
  • Data
  • Fields
  • Path
  • Component
  • Formatter & Parser
  • Dependencies
  • Required
  • Validators
  • Exclude Term
  • Disable Term
  • Require Term
  • Dependencies Change
  • Context
  • Dirty
  • Invalid
  • Errors
  • Processing
  • Pending Actions
  • Hooks
  • Term
  • Log
  • Error Codes
  • Actions
  • Definition Shorthand

Advanced Usages

  • Grid Usage
  • Form Persistency
  • Undo & Redo
  • Replay Actions
  • Track Actions
  • Server Validation
  • Entity Templates
  • Folder Structure

React Form

  • Installation
  • Overview
  • Form
  • Field
  • Components
  • Layout
  • Demos

Tools

  • Editor
  • DevTools

More Info

  • Test Cofi
  • Packages
  • Why Cofi?
  • Technologies
  • Videos
  • Channels

Contribute

  • Code of Conduct
  • Contributing Guide
  • Contributors

Form Persistency

Form persistency can be accomplished by the following steps:

  • Define a form model definition as a first form state and pass it to the Form component. It will be updated during the form's lifecycle.
  • Define an afterAction hook to get the updated form model after each action and store it in a local-storage.
import { Actions } from '@cofi/form';

const localStorageKey = 'user-form';

const saveFormToLocalStorage = ({ model, type }) => {
  if (type !== Actions.DESTROY) {
    localStorage.setItem(localStorageKey, JSON.stringify(model));
  }
};

resources = {
  //...
  hooks: {
    afterAction: saveFormToLocalStorage,
  },
};
  • On page load (on refresh) - restore the form model object from the local storage and pass it to the Form component in order to go back to the last state of the form - before the refresh.
import form from './form';

const getModel = () => {
  // Retrieve the model from storage
  const prevModelStringify = localStorage.getItem(localStorageKey);
  return prevModelStringify ? JSON.parse(prevModelStringify) : form.model;
};

const model = getModel();

<Form model={model} resource={form.resources}>

See advanced demo in Form Persistency Demo.

← Grid UsageUndo & Redo →
cofi
Copyright © 2021 Verizon Media
DOCS
IntroductionForm ClassReact FormReact ComponentsReact Layout
DEMOS
React FormReact ComponentsReact Layout
CONTRIBUTING
Code Of ConductContributing GuideContributorsUsers
CHANNELS
StarGitHubStack OverflowMediumVideos