Stan-js

A lightweight and flexible state management library designed for use in Rea...

README

stan-js


Overview


stan-js is a lightweight and flexible state management library designed for use in React, React Native and even vanilla-js applications. It simplifies the process of managing state in your application by providing a simple createStore function. This package aims to offer a straightforward solution for state management without the need for extensive type definitions or repetitive code.

Features


- ⚡️ Performance and minimal re-renders
- ✍️ Simple configuration
- ⚛️ Out of the box React integration
- 💾 Built in data persistence
- 🚀 Amazing typescript IntelliSense
- 🪝 Easy access to all store values
- 🪶 Very lightweight
- 🥳 No dependencies
- 🛡️ 100% Test coverage

Installation


Install package using preferred package manager:

  1. ```bash
  2. npm install stan-js
  3. yarn add stan-js
  4. pnpm add stan-js
  5. bun add stan-js
  6. ```

Getting Started


Create a store with initial state:

You can think of a store as your app state. You can define multiple keys/values, each key will create separated subscription (more explained here). If you want to persist the value - you can simply wrap it in Synchronizer

  1. ```typescript
  2. import { createStore } from 'stan-js'
  3. import { storage } from 'stan-js/storage'

  4. export const { useStore } = createStore({
  5.     count: 0,
  6.     get doubleCount() {
  7.         return this.count * 2
  8.     },
  9.     user: storage(''),
  10.     selectedLanguage: 'en-US',
  11.     unreadNotifications: [] as Array<Notification>
  12. })
  13. ```

Use the returned hook in your React component:

  1. ```typescript
  2. import { useStore } from './store'

  3. const App = () => {
  4.     const { count, user, setCount } = useStore()

  5.     return (
  6.         <div>
  7.             <h1>Hello {user}!</h1>
  8.             <p>Count: {count}</p>
  9.             <button onClick={() => setCount(prev => prev + 1)}>Increment</button>
  10.             <button onClick={() => setCount(prev => prev - 1)}>Decrement</button>
  11.         </div>
  12.     )
  13. }
  14. ```

Check out our Docs for more information and examples.