Skip to content

Introduction

Installation

To install stan-js run the following command in your terminal:

Terminal window
npm install stan-js

Create a store

To create a store, you need to import the createStore function from stan-js and pass it an object with the initial state.

Create a new store:

store.ts
import { createStore } from 'stan-js'
import { storage } from 'stan-js/storage'
export const { useStore } = createStore({
count: 0,
get doubleCounter() {
return this.counter * 2
},
user: storage(''),
selectedLanguage: 'en-US',
unreadNotifications: [] as Array<Notification>
})

Use the returned hook in your React component:

App.tsx
import { useStore } from './store'
const App = () => {
const { count, user, setCount } = useStore()
return (
<div>
<h1>Hello {user}!</h1>
<p>Count: {count}</p>
<button onClick={() => setCount(prev => prev + 1)}>Increment</button>
<button onClick={() => setCount(prev => prev - 1)}>Decrement</button>
</div>
)
}