Get and put objects from/to local storage. They will be (de)serialized as JSON, so be sure that's OK for your objects.
If localStorage is not available, all methods essentially act as noops.
// if 'my key' has never been used before const persistence = new Persistence('my key'); persistence.get(); // returns `undefined` // now you set it persistence.put({ name: 'Robert' }); // this will work even after the app reloads (e.g. the next week) persistence.get(); // returns { name: 'Robert' } Copy
// if 'my key' has never been used before const persistence = new Persistence('my key'); persistence.get(); // returns `undefined` // now you set it persistence.put({ name: 'Robert' }); // this will work even after the app reloads (e.g. the next week) persistence.get(); // returns { name: 'Robert' }
The key in local storage at which to find the existing object (if any), and to save it.
Deletes the saved item from local storage.
Retrieves a deserialized copy of the saved object, or undefined if it has not been set.
undefined
Serializes obj and saves it in local storage.
obj
Get and put objects from/to local storage. They will be (de)serialized as JSON, so be sure that's OK for your objects.
If localStorage is not available, all methods essentially act as noops.