Skip to content

createStore ​

Creates a store with state management capabilities

helpers
test coverage
Last changed: 24 days ago

Installation ​

Library
CLI
Manual
typescript
import { createStore } from '@siberiacancode/reactuse';

Usage ​

typescript
const { set, get, use, subscribe } = createStore((set) => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) }));

Demo ​

Api ​

Parameters

NameTypeDefaultNote
createStateStateCreator<Value>-- Function that initializes the store state

Returns

StoreApi<Value>

Type declaration ​

typescript
type SetStateAction<Value> = ((prev: Value) => Value) | Value;

type StateCreator<Value> = (
  set: (action: SetStateAction<Value>) => void,
  get: () => Value
) => Value;

export interface StoreApi<Value> {
  getInitialState: () => Value;
  getState: () => Value;
  setState: (action: SetStateAction<Value>) => void;
  subscribe: (listener: (state: Value, prevState: Value) => void) => () => void;
}

type Listener = (state: Value, prevState: Value) => void;

Source ​

Source • Demo

Contributors ​

D
debabin
debabin

Released under the MIT License.