Skip to content

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

License

Notifications You must be signed in to change notification settings

glyph-cat/cotton-box

Repository files navigation

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

About

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

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published