Skip to main content

Local Storage

The Local Storage Packages is part of FlexNative, that provides an asynchronous, unencrypted, persistent, key-value storage API.

Local Storage can only store string data. In order to store object data, you need to serialize it first. For data that can be serialized to JSON, you can use JSON.stringify() when saving the data and JSON.parse() when loading the data.

With @flexnative/local-storage you dont needs to serialize and deserialize strings to save or get object from local storage, @flexnative/local-storage does it for you.

Installation

You can installing FlexNative Local Storage packages using npm:

npm i @flexnative/local-storage

Dependencies

Examples

Save values on local storage

To save values ​​on local storage you only need to pass the key and the value where the value can be a string or any object. If the value you needs to store in local storage it is an object, you just need pas the value without the need to serialize it first, the package does it for you.

If you are using typescript you also needs to pass the object type.

import Storage from '@flexnative/local-storage';

//saveing a string
await Storage.saveItem('string-value-key', 'My string VALUE');

//saveing an object
await Storage.saveItem('object-value-key', {id: 1, name: 'Redon', lastName: 'Alla'});

Get values from local storage

To get values from local storage you only need to pass the key and and an default value to be returned if the value it is not found in local storage. The value can be a string or any object. To get the value you just need pas the key and defaultvalue without the need to serialize it first, the package does it for you. If you are using typescript you also needs to pass the object type.

import Storage from '@flexnative/local-storage';

//saveing a string
const myValue = await Storage.getItem('string-value-key', 'My string VALUE');

//saveing an object
const author = await Storage.getItem('object-value-key', {id: 1, name: 'Redon', lastName: 'Alla'});

Deleting values from local storage

To delete values from local storage you just need to pas the key.

import Storage from '@flexnative/local-storage';

await Storage.deleteItem('string-value-key');
await Storage.deleteItem('object-value-key');