ilokesto

Utils

Essential utilities for store composition and state updates.

Utils

The @ilokesto/state/utils entry point provides two main helpers: pipe and adaptor. These are designed to make store setup and state management more ergonomic.

pipe

pipe is a utility for composing middleware around an initial state. It applies middleware from left to right (top to bottom) and returns a fully initialized Store instance.

import { create } from '@ilokesto/state';
import { pipe } from '@ilokesto/state/utils';
import { logger, devtools } from '@ilokesto/state/middleware';

const store = pipe(
  { count: 0 },
  logger(),
  devtools('MyStore')
);

const useStore = create(store);

Using pipe is the recommended way to set up complex stores with multiple middleware.

adaptor

adaptor is a thin wrapper around Immer's produce. It lets you write a draft-style state recipe and get back a pure updater function.

import { adaptor } from '@ilokesto/state/utils';

type State = { user: { profile: { name: string } } };

const renameProfile = adaptor<State>((draft) => {
  draft.user.profile.name = 'New Name';
});

useStore.writeOnly()(renameProfile);

Installation

adaptor requires immer to be installed in your project.

npm install immer
  • create: Main entry point for React integration.
  • middleware: Built-in middleware for common tasks.

On this page