Skip to content

Quick Start

Create a store with initial state:

import { createStore } from '@standard-store/store';
// Create a store with initial state
const store = createStore({
count: 0,
name: 'John',
});
// Get current state
const state = store.getState();
console.log(state); // { count: 0, name: 'John' }
// Update state
store.setState({ count: 1 });
// Subscribe to changes
const unsubscribe = store.subscribe((state) => {
console.log('State updated:', state);
});
// Unsubscribe when needed
unsubscribe();

Create a custom hook for React components:

import { createStoreHook } from '@standard-store/react';
// Create a custom hook
const useStore = createStoreHook(store);
function MyComponent() {
const state = useStore();
return (
<div>
<p>Count: {state.count}</p>
<p>Name: {state.name}</p>
<button onClick={() => store.setState({ count: state.count + 1 })}>
Increment
</button>
</div>
);
}

Persist state to localStorage automatically:

import { createStore } from '@standard-store/store';
import { localStorageAdapter } from '@standard-store/adapters';
const store = createStore(
{
count: 0,
name: 'John',
},
{
adapter: localStorageAdapter('my-app-store'),
}
);
// State will automatically persist to localStorage
// and restore on page reload!