createStore ​
Creates a store with state management capabilities
helpers
medium
test coverage
Last changed: 7 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
| Name | Type | Default | Note |
|---|---|---|---|
| createState | StateCreator<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> {
get: () => Value;
getInitial: () => Value;
set: (action: StoreSetAction<Value>) => void;
subscribe: (listener: StoreListener<Value>) => () => void;
use: (() => Value) &
(<Selected>(selector: (state: Value) => Selected) => Selected) &
(<Selected>(selector?: (state: Value) => Selected) => Selected | Value);
}Source ​
Source • DemoContributors ​
D
Y