mobx-stored
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

mobx-stored

a simple utility for persisting POJO objects into localStorage via mobx observables. Not only they persist through sessions, they also synchronize via storage events across all browser tabs.

usage

import { localStored } from 'mobx-stored'
import { sessionStored } from 'mobx-stored' // for using sessionStorage rather than localStorage
 
const defaultUser = { email: null, firstname: null, lastname: null }
const observableUserProfile = localStored('userProfile', defaultUser, {
  delay: 500
}) // last parameter is optional-mobx autorun options for running the save operation into the storage. Use higher delay if you store a lot of data
 
// now any changes made to the observableUserProfile are synced in localStorage
 
observableUserProfile.name = 'Michael'
 
// after 500ms and reloading the page
 
observableUserProfile.name === 'Michael' // true
 
// revert to the default values
 
observableUserProfile.reset()
 
// need to add new properties?
 
observableUserProfile.extend({
  myNewProp: 1
})
 
//Don't need it anymore?
 
observableUserProfile.destroy() // removes it from localStorage

Package Sidebar

Install

npm i mobx-stored

Weekly Downloads

310

Version

1.1.0

License

MIT

Unpacked Size

16.2 kB

Total Files

15

Last publish

Collaborators

  • capaj