If your project requires using web storage (either localStorage or sessionStorage) to persist data, and you want the data to be retained after a page refresh while also automatically updating the view when the data changes, ew-responsive-store is the perfect solution.
It's about 4 KB gzipped and extremely easy to use. With just a single function call, you can make storage data reactive, which can be applied to any framework-based project, even native JavaScript projects. The library also includes comprehensive unit tests and type inference.
First, you need to install the ew-responsive-store package. You can install it using the following command:
Since v0.0.3, ew-responsive-store supports multiple frameworks. You only need to install the framework you're using:
Note: Framework dependencies are treated as external, so they won't be bundled with your application, keeping the library size minimal.
useSyncExternalStore, and peerDependencies requires react >= 18. React 17 and earlier are not supported.ew-responsive-store are installed (multiple versions, a monorepo without hoisting, or a package manager that did not dedupe), the registry is split and components sharing the same key will not sync. Run npm dedupe / pnpm dedupe / yarn dedupe, or configure resolve.dedupe: ['ew-responsive-store'] in Vite.releaseStorageManager(key, storage); the instance is removed from the registry and destroyed in a microtask. When using Vue outside setup, wrap the call in effectScope() and call scope.stop() to release automatically. In Angular, call it within an injection context (component/service), otherwise release manually.Since v0.0.3, you should import from framework-specific entry points:
The root entry of ew-responsive-store exports the vanilla JavaScript useStorage, along with StoreType, ParseStrType, the StorageManager APIs, and utilities such as parseStr, isValidJSON, and isStorageEnabled. The root useStorage turns localStorage / sessionStorage data into a reactive object; for Vue, React, and other frameworks, use their dedicated subpath entries.
You can use useStorage to create reactive basic values. For example, let's say you have a counter stored in localStorage:
Vue Template Code:
At this point, the value of count is stored in the browser's session storage, and it is reactive, meaning it will persist even after the page refreshes and the view will update automatically when the value changes.
You can also store reactive objects in a similar way:
When you change the name property of userInfo, the view will automatically update, and the data will be saved in session storage.
You can also store arrays, and they will be reactive as well:
The Vue adapter releases the storage manager on unmount via onScopeDispose. When you call it outside setup or an effectScope (e.g. a module-level singleton, Pinia store initialization, or a utility function), there is no lifecycle to hook into, so create an explicit scope:
Notes:
scope.stop() also stops watchers and reactive effects inside the scope; afterwards settings is no longer synced with storage.effectScope is not an option, release manually. The key and storage area must match the ones used at creation:useStorage directly inside a component's setup needs no extra handling; unmount releases it automatically.For more advanced usage and configuration options, check out the API Reference section.