Minimal State Management Library For React
- Minimal state management core
- React integration with
useSyncExternalStore
- TypeScript support
npm install obostha
Create a store using createStore:
import { createStore } from 'obostha'
type CounterState = { count: number }
const counterStore = createStore<CounterState>((set) => ({
count: 0,
increment: () => set((s) => ({ count: s.count + 1 })),
}))
createStore(fn)
initializes the store with your state and actions.- The returned store has:
getSnapshot()
: returns the current statesubscribe(listener)
: subscribe to state changessetState(partial)
: update the state
Use create
to create a React hook for your store:
import { create } from 'obostha'
type CounterState = { count: number }
const useCounter = create<CounterState>((set) => ({
count: 0,
increment: () => set((s) => ({ count: s.count + 1 })),
}))
function Counter() {
const count = useCounter((s) => s.count)
const increment = useCounter((s) => s.increment)
return (
<button onClick={increment}>
Count: {count}
</button>
)
}
create(fn)
returns a React hook.- The hook accepts a selector to pick part of the state.
function createStore<S extends object>(
fn: StateCreator<S>
): {
getSnapshot: () => S
subscribe: (listener: () => void) => () => void
setState: SetState<S>
}
StateCreator<S>
:(set: SetState<S>) => S
SetState<S>
:(partial: Partial<S> | (prev: S) => Partial<S>) => void
function create<S extends object>(fn: StateCreator<S>): <Sel = S>(selector?: (s: S) => Sel) => Sel
- Returns a React hook for consuming the store.
MIT