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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-native-hox

v1.0.1

Published

一个轻量级、类型安全、零心智负担的 React Native 状态管理解决方案。

Downloads

377

Readme

react-native-hox

面向 React Native 的极简状态管理:类型安全、API 零心智负担,单文件架构,包体积极小。

NPM Version TypeScript License


亮点

| 特性 | 说明 | |------|------| | 包体积极小 | 单入口构建,gzip 后 ESM ~1.5 KB / CJS ~1.6 KB;运行时仅依赖 use-sync-external-store,持久化可选依赖 AsyncStorage | | 类型安全 | 全 TypeScript,createModel 泛型推导完整,selector 与 setState 类型即文档 | | API 极简 | 一个 createModel 搞定:组件内用 Hook,组件外用 model.data,无需 Provider | | 按需重渲染 | 支持 selector + 可选 shallow,只在关注字段变化时更新组件 | | 可选持久化 | 内置 persist(默认 AsyncStorage),支持自定义 storage、防抖、迁移钩子 | | React 18 友好 | 基于 useSyncExternalStore,无 tearing,兼容并发渲染 |


快速开始

安装

npm i react-native-hox @react-native-async-storage/async-storage

项目中已有 @react-native-async-storage/async-storage 则无需重复安装。

最小示例

// stores/user.ts
import { createModel } from 'react-native-hox';

export const userStore = createModel({ name: 'Guest' });
// 持久化:createModel({ name: 'Guest' }, { persist: 'user_v1' });
// Profile.tsx
import React from 'react';
import { View, Text, Button } from 'react-native';
import { userStore } from './stores/user';

export function Profile() {
  const user = userStore.getState();
  return (
    <View>
      <Text>Hello, {user.name}</Text>
      <Button title="Set Name" onPress={() => userStore.setState({ name: 'Tom' })} />
    </View>
  );
}

功能概览

1) 更新状态:自动合并 / 显式替换

const profileStore = createModel({ name: 'Guest', role: 'user' as 'user' | 'admin' });
profileStore.setState({ name: 'Alice' });                    // 合并
profileStore.setState({ name: 'Bob', role: 'admin' }, true);  // 整体替换

2) Selector:只在关注数据变化时重渲染

const count = counterStore.getState((s) => s.count);
// 返回对象时建议用 shallow
const picked = counterStore.getState((s) => ({ count: s.count }), shallow);

3) 组件外读写:Vanilla API

const name = userStore.data.state.name;
userStore.data.getState();
userStore.data.subscribe((next, prev) => {});

4) 持久化

createModel({ token: '' }, { persist: 'auth_v1' });
// 或自定义 storage / beforeRecover / beforePersist
createModel({ token: '' }, { persist: { key: 'auth_v1', storage: myStorage } });

5) reset / destroy / strict

userStore.reset();
userStore.destroy();
createModel({ count: 0 }, { strict: { forbidSetStateAfterDestroy: true } });

进阶

subscribe(selector):按 slice 订阅

userStore.data.subscribe(
  (s) => s.name,
  (next, prev) => console.log(next, prev),
  { fireImmediately: true }
);

持久化迁移

createModel(
  { token: '', isLogin: false },
  {
    persist: {
      key: 'user_v1',
      beforeRecover: (v) => ({ token: v?.token ?? '', isLogin: Boolean(v?.token) }),
    },
  }
);

初始状态用函数

createModel(() => ({ bootAt: Date.now() }));

API 简表

| 方法 / 属性 | 说明 | |-------------|------| | model() / model.getState() / model.use() / model.useState() | 组件内 Hook,可传 selector + equalityFn | | model.setState(partial, replace?) | 更新状态 | | model.data.state / model.data.getState() | 组件外读状态 | | model.data.setState / model.data.subscribe | 组件外写与订阅 | | model.reset() / model.destroy() | 重置为初始值、销毁 |

optionspersist(string 或 { key, storage?, debounce?, beforePersist?, beforeRecover? })、loggerstrict.forbidSetStateAfterDestroy


License

ISC