Introduction
Installation
To install stan-js run the following command in your terminal:
npm install stan-js
yarn add stan-js
pnpm add stan-js
bun add 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:
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:
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> )}