A TypeScript-based local storage service for managing data in the browser's local storage.
Using npm
:
npm install typed-local-storage-service --save
import LocalStorageService from 'typed-local-storage-service'
interface IUser {
name: string
age: number
}
interface ILocalStorage {
user: IUser
test: string
}
export const useLocalStorage = new LocalStorageService<ILocalStorage>()
This package provides a type-safe way to interact with the browser's localStorage
. It ensures that the values stored are correctly typed, making it easier to manage data across your application.
Stores a value under the specified key in local storage. The value is serialized to JSON.
Example
useLocalStorage.setItem('user', { name: 'Alice', age: 30 });
Retrieves the value associated with the specified key. The value is deserialized from JSON.
Example
const user = useLocalStorage.getItem('user');
console.log(user); // { name: 'Alice', age: 30 }
Removes the specified key from local storage.
Example
useLocalStorage.removeItem('user');
Clears all items in local storage.
Example
useLocalStorage.clear();
MIT