Skip to content

@acoolhq/react-tiny-store


@acoolhq/react-tiny-store / createStoreSlice

Function: createStoreSlice()

createStoreSlice<T, S, FNS>(store, select, fns): () => object

Defined in: hooks.ts:168

Create a slice hook bound to a store; actions are pure (no side effects) and must return the next root state.

Type Parameters

T

T

Root state.

S

S

Selected slice.

FNS

FNS extends RootFns<T>

Pure updater map (root, ...args) => nextRoot.

Parameters

store

Store<T>

The external store.

select

(root) => S

(root) => slice used for state and get().

fns

FNS

Pure updaters; each must return a new root (T).

Returns

useSlice(): { state, get, actions } where actions[key](...args): void

(): object

Returns

object

actions

actions: Bound<FNS>

get()

get: () => S

Returns

S

state

state: S

Example

ts
const useTodos = createStoreSlice(store, s => s.todos, {
  add(root, text: string) {
    return { ...root, todos: [...root.todos, { id: crypto.randomUUID(), text }] };
  }
});