cotton-box
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

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

This is the core package of cotton-box.
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

Readme

Keywords

Package Sidebar

Install

npm i cotton-box

Weekly Downloads

13

Version

0.2.0

License

MIT

Unpacked Size

198 kB

Total Files

18

Last publish

Collaborators

  • glyph-cat