stored-state
v1.0.2
Published
Simplest persistent state for browser apps
Maintainers
Readme
stored-state
Simplest persistent state for browser apps
- State cached in memory and synced to localStorage
- Fast reads and state survive reloads
- Zero setup, zero dependencies
import { state } from "stored-state";
state.user = { name: "peter" };
state.token = "1234";
state.theme = "dark";Install
npm install stored-stateUpdate State
Update state by assignment. Values can be any primitives, objects or arrays. Always assign top level keys, not nested fields.
import { state } from "stored-state";
state.theme = "dark";
state.layout = {
lastViewed: "analytics"...
};
// NOT do
state.user.lastViewed = ... Read state
Read state like a normal object. State can be accessed and modified anywhere in your app.
import { state } from "stored-state";
console.log(state);
if (state.theme === "dark") ...Clear State
Clear single value. Two options:
delete state.theme;
state.theme = null;Clear whole state
state.clear();Advantages
- Simple:
state.name = value - Fast: values cached in memory after first access
- Small: ~750 bytes min+gzip
- Zero setup, zero dependencies
License
MIT
Hint
If you don't want manually import { state } from "stored-state" many times,
you can just add this import at app start.
import "stored-state/global"
// Now you can access STATE anywhere in your app without imports
STATE.user = {...}