Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 2.88 KB

README.md

File metadata and controls

78 lines (53 loc) · 2.88 KB

Banner

A flexible toolbox with different types of state managers catered to different scenarios.

License Open in Visual Studio Code Support me on Ko-fi

Packages Stats
cotton-box Version NPM Unpacked Size
cotton-box-react Version NPM Unpacked Size

Key Features

  • Lightweight & fast
  • Declarative lifecycle management
  • Supports asynchronous set-state functions
  • Official bindings for React available

Documentation

View documentations (still a work-in-progress)


Examples

Plain Example

import { StateManager } from 'cotton-box'

const CounterState = new StateManager(42)

// Set, get, reset
CounterState.set(7)
console.log(CounterState.get()) // 7
CounterState.reset()
console.log(CounterState.get()) // 42

// Watch for state changes
const unwatch = CounterState.watch((state) => {
  console.log(state)
})

// Wait for state to change
// Promise will resolve when value matches
await CounterState.wait(50)

Example With React

import { useStateValue } from 'cotton-box-react'

function App() {
  const counter = useStateValue(CounterState)
  return <h1>Counter: {counter}</h1>
}

Support This Project