npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mobx-chunk

v1.2.2

Published

Composable MobX store factory and React hooks

Downloads

49

Readme

mobx-chunk

npm version Downloads

A lightweight, type-safe factory for building MobX-powered state slices ("chunks") with auto-generated actions, selectors, async flows, loading flags, and optional persistence.

Full Documentation

Features

  • Automatic Actions & Selectors: Generates set${ValueKey} actions and get${ValueKey} selectors for each state field.
  • Async Actions & Loading Flags: Define asynchronous flows with store.asyncActions and track them via store.isLoading flags.
  • Type Safety: Fully typed stores, actions, and selectors using TypeScript inference.
  • Persistence: Plug in any storage engine (MMKV, AsyncStorage, localStorage) to persist specific fields.
  • React Integration: React hooks (useValues, useComputed, useChunk) for seamless UI updates.

Installation

# with npm
npm install mobx-chunk

# with yarn
yarn add mobx-chunk

Quick Start

Basic Example

// store.ts
import { createChunk } from "mobx-chunk";

export type TState = { accessToken: string };
export const store = createChunk<TState>({
  name: "store",
  initialState: { accessToken: "" } satisfies TState,
  persist: ["accessToken"],
});

// App.tsx
import React from "react";
import { View, Text } from "react-native";
import { useValues } from "mobx-chunk";
import { store } from "./store";

export default function App() {
  const { accessToken } = useValues({
    accessToken: () => store.selectors.getAccessToken,
  });

  return (
    <View>
      <Text>{accessToken}</Text>
    </View>
  );
}

Create a Chunk

Define a chunk with custom actions, async flows, and views:

import { createChunk } from "mobx-chunk";
import { actions, type TActions } from "./actions";
import { asyncActions, type TAsyncActions } from "./asyncActions";
import { selectors, type TSelectors } from "./selectors";

export type Todo = { id: number; title: string; isComplete: boolean };
export type TState = { todoList: Todo[] };

export const todoStore = createChunk<
  TState,
  TActions,
  TAsyncActions,
  TSelectors
>({
  name: "todo",
  initialState: { todoList: [] } satisfies TState,
  persist: ["todoList"],
  actions,
  asyncActions,
  views: selectors,
});

Subscribe to Changes

Use React hooks to reactively subscribe:

import { useValues, useComputed } from "mobx-chunk";
import { todoStore } from "./todo-store";

// Batch subscription
const { todoList, isLoading } = useValues({
  todoList: () => todoStore.selectors.getTodoList,
  isLoading: () => todoStore.isLoading.asyncFunctionExample,
});

// Single subscription
const singleList = useComputed(
  () => todoStore.selectors.getTodoList
);

Persistence Setup

Configure a storage engine in your app entrypoint:

import { configurePersistenceEngine } from "mobx-chunk";
import { MMKV } from "react-native-mmkv";

const storage = new MMKV();
configurePersistenceEngine({
  clear: () => storage.clearAll(),
  get:    (key) => storage.getString(key),
  remove: (key) => storage.delete(key),
  set:    (key, value) => storage.set(key, value),
});

Supports synchronous or asynchronous APIs (e.g., AsyncStorage or browser localStorage).

Middleware (Interceptors)

Add global interceptors for validation, logging, or metrics:

import { addActionInterceptor } from "mobx-chunk";

addActionInterceptor((ctx, next) => {
  // ctx.chunkName, ctx.actionName, ctx.args
  if (ctx.actionName === "yourAction") {
    // validate or log
  }
  return next();
});

Coming Soon: Separate interceptors for general actions, async actions, and sync actions.

License

This project is licensed under the MIT License.