@ariakit/store

中文文档
0.1.11发布于 2026/10/14MITTypeScriptES Module构建来源已验证近 30 天下载 343.7万

Ariakit store utilities

原始使用文档
文档目录19 节
  1. Contents
  2. Installation
  3. Usage
  4. API reference
  5. createStore
  6. setup
  7. init
  8. subscribe
  9. sync
  10. batch
  11. omit
  12. pick
  13. mergeStore
  14. throwOnConflictingProps
  15. State
  16. StoreOptions
  17. StoreProps
  18. StoreState
  19. Store

@ariakit/store

Important: This package is experimental and does not follow semantic versioning, meaning breaking changes may occur in patch and minor versions.

Framework-agnostic store primitives used by Ariakit packages.

Contents

Installation

npm i @ariakit/store

Usage

Import store helpers from the package root:

import { createStore } from "@ariakit/store";

This package is ESM-only and exposes a single public entrypoint.

API reference

createStore

function createStore<S extends State>(
  initialState: S,
  ...stores: Array<Store<Partial<S>> | undefined>
): Store<S>;

Creates a store.

setup

type StoreSetup = (callback: () => void | (() => void)) => () => void;

function setup<T extends Store>(
  store?: T | null,
  ...args: Parameters<StoreSetup>
): T extends Store ? ReturnType<StoreSetup> : void;

Register a callback function that's called when the store is initialized.

init

type StoreInit = () => () => void;

function init<T extends Store>(
  store?: T | null,
  ...args: Parameters<StoreInit>
): T extends Store ? ReturnType<StoreInit> : void;

Function that should be called when the store is initialized.

subscribe

type Listener<S> = (state: S, prevState: S) => void | (() => void);

type Sync<S, K extends keyof S> = (
  keys: K[] | null,
  listener: Listener<Pick<S, K>>,
) => () => void;

type StoreSubscribe<S = State, K extends keyof S = keyof S> = Sync<S, K>;

function subscribe<T extends Store, K extends keyof StoreState<T>>(
  store?: T | null,
  ...args: Parameters<StoreSubscribe<StoreState<T>, K>>
): T extends Store ? ReturnType<StoreSubscribe<StoreState<T>, K>> : void;

Registers a listener function that's called after state changes in the store.

sync

type Listener<S> = (state: S, prevState: S) => void | (() => void);

type Sync<S, K extends keyof S> = (
  keys: K[] | null,
  listener: Listener<Pick<S, K>>,
) => () => void;

type StoreSync<S = State, K extends keyof S = keyof S> = Sync<S, K>;

function sync<T extends Store, K extends keyof StoreState<T>>(
  store?: T | null,
  ...args: Parameters<StoreSync<StoreState<T>, K>>
): T extends Store ? ReturnType<StoreSync<StoreState<T>, K>> : void;

Registers a listener function that's called immediately and synchronously whenever the store state changes.

batch

type Listener<S> = (state: S, prevState: S) => void | (() => void);

type Sync<S, K extends keyof S> = (
  keys: K[] | null,
  listener: Listener<Pick<S, K>>,
) => () => void;

type StoreBatch<S = State, K extends keyof S = keyof S> = Sync<S, K>;

function batch<T extends Store, K extends keyof StoreState<T>>(
  store?: T | null,
  ...args: Parameters<StoreBatch<StoreState<T>, K>>
): T extends Store ? ReturnType<StoreBatch<StoreState<T>, K>> : void;

Registers a listener function that's called immediately and after a batch of state changes in the store.

omit

type StoreOmit<
  S = State,
  K extends ReadonlyArray<keyof S> = ReadonlyArray<keyof S>,
> = (keys: K) => Store<Omit<S, K[number]>>;

function omit<T extends Store, K extends ReadonlyArray<keyof StoreState<T>>>(
  store?: T | null,
  ...args: Parameters<StoreOmit<StoreState<T>, K>>
): T extends Store ? ReturnType<StoreOmit<StoreState<T>, K>> : void;

Creates a new store with a subset of the current store state and keeps them in sync.

pick

type StorePick<
  S = State,
  K extends ReadonlyArray<keyof S> = ReadonlyArray<keyof S>,
> = (keys: K) => Store<Pick<S, K[number]>>;

function pick<T extends Store, K extends ReadonlyArray<keyof StoreState<T>>>(
  store?: T | null,
  ...args: Parameters<StorePick<StoreState<T>, K>>
): T extends Store ? ReturnType<StorePick<StoreState<T>, K>> : void;

Creates a new store with a subset of the current store state and keeps them in sync.

mergeStore

function mergeStore<S extends State>(
  ...stores: Array<Store<S> | undefined>
): Store<S>;

Merges multiple stores into a single store.

throwOnConflictingProps

function throwOnConflictingProps(props: AnyObject, store?: Store): void;

Throws when a store prop is passed in conjunction with a default state.

State

type State = AnyObject;

Store state type.

StoreOptions

type StoreOptions<S extends State, K extends keyof S> = Partial<Pick<S, K>>;

Initial state that can be passed to a store creator function.

StoreProps

interface StoreProps<S extends State = State> {
  /**
   * Another store object that will be kept in sync with the original store.
   *
   * Live examples:
   * - [Navigation Menubar](https://ariakit.com/examples/menubar-navigation)
   */
  store?: Store<Partial<S>>;
}

Props that can be passed to a store creator function.

StoreState

type StoreState<T> = T extends Store<infer S> ? S : never;

Extracts the state type from a store type.

Store

interface Store<S = State> {
  /**
   * Returns the current store state.
   */
  getState(): S;
  /**
   * Sets a state value.
   */
  setState<K extends keyof S>(key: K, value: SetStateAction<S[K]>): void;
}

Store.

版本与发布记录

@ariakit/store 共发布 12 个版本,首次发布于 2026/05/23;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.11latest2026/10/1424 天前
0.1.102026/09/151 个月前
0.1.92026/08/242 个月前
0.1.82026/08/102 个月前
0.1.72026/07/243 个月前
0.1.62026/07/153 个月前

关于 @ariakit/store 的常见问题

@ariakit/store 是什么?

Ariakit store utilities

如何安装 @ariakit/store?

在项目目录运行 npm install @ariakit/store,或使用 pnpm add / yarn add / bun add。

@ariakit/store 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ariakit/store 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ariakit/store 可以商用吗?

@ariakit/store 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ariakit/store 的下载量有多少?

根据 npm 官方下载统计,@ariakit/store 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,437,498 次,日均约 114,583 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ariakit/store 还在维护吗?

最新版本 0.1.11 发布于 2026/10/14(24 天前);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2026/05/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。