Skip to content

createStore ​

Creates a store with state management capabilities

helpers
medium
test coverage
Last changed: 9 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 StoreSetAction<Value> = ((prev: Value) => Partial<Value>) | Partial<Value>;

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

type StoreCreator<Value> = (
  set: (action: StoreSetAction<Value>) => void,
  get: () => Value
) => Value;

export interface StoreApi<Value> {
  getInitialState: () => Value;
  getState: () => Value;
  setState: (action: StoreSetAction<Value>) => void;
  subscribe: (listener: StoreListener<Value>) => () => void;
}

Source ​

Source • Demo

Contributors ​

D
debabin
debabin
Y
y0na24
y0na24

Released under the MIT License.