@bigcommerce/data-store

中文文档
1.0.3发布于 2025/09/05MITTypeScriptCommonJS近 30 天下载 8965

A JavaScript library for managing application state

原始使用文档
文档目录9 节
  1. Install
  2. Requirements
  3. Usage
  4. Create a store
  5. To update the current state
  6. To subscribe to changes
  7. To transform states and actions
  8. Contribution
  9. License

@bigcommerce/data-store

Build Status

A JavaScript library for managing application state.

It helps you to enforce unidirectional data flow in your application, by allowing you to:

  • Subscribe to changes to the application state
  • Update the state in a serial and immutable fashion

Install

You can install this library using npm.

npm install --save @bigcommerce/data-store

Requirements

This library requires Promise polyfill if you need to support older browsers, such as IE11.

You may need to create Observables when using this library (please refer to the usage section). We recommend you to use rxjs until the time comes when you can create them natively.

Usage

Create a store

import { createDataStore } from '@bigcommerce/data-store';

const reducer = (state, action) => {
    switch (action.type) {
    case 'INCREMENT':
        return { ...state, count: state.count + 1 };

    case 'UPDATE_COUNT':
        return { ...state, count: action.payload };

    default:
        return state;
    }
};

const initialState = { count: 0 };
const store = createDataStore(reducer, initialState);

To update the current state

import { createAction } from '@bigcommerce/data-store';

store.dispatch(createAction('INCREMENT'));
store.dispatch(createAction('UPDATE_COUNT', 10)));

To update the state asynchronously, you need to create an observable that emits actions:

import { Observable } from 'rxjs';

const action$ = Observable
    .ajax({ url: '/count' })
    .map(({ response }) => createAction('UPDATE_COUNT', response.count))

store.dispatch(action$);

To avoid race condition, actions get dispatched in a series unless you specify a different dispatch queue, i.e.:

store.dispatch(action$);
store.dispatch(action$);

// The following call does not wait for the previous calls
store.dispatch(action$, { queueId: 'foobar' });

Wrap the observable in a closure if you want to access the store elsewhere but don't have direct access to it (i.e.: inside an action creator):

// In an action creator
function updateAction() {
    return (store) => Observable
        .ajax({ url: '/count' })
        .map(({ response }) => {
            const { count } = store.getState();

            return createAction('UPDATE_COUNT', count + response.count);
        });
}
// In a component
store.dispatch(updateAction());

To do something after an asynchronous dispatch:

const { state } = await store.dispatch(action$);

console.log(state);

To subscribe to changes

To changes and render the latest data:

store.subscribe((state) => {
    console.log(state);
});

The subscriber will get triggered once when it is first subscribed. And it won't get triggered unless there's a data change.

To filter out irrelevant changes:

// Only trigger the subscriber if `count` changes
store.subscribe(
    (state) => { console.log(state); },
    (state) => state.count
);

To transform states and actions

To transform the return value of getState or parameter value of subscribe:

const stateTransformer = (state) => ({ ...state, transformed: true });
const store = createDataStore(reducer, initialState, { stateTransformer });

console.log(store.getState()); // { count: 0, transformed: true }

To transform dispatched actions:

const actionTransformer = (action) => ({ ...action, transformed: true });
const store = createDataStore(reducer, initialState, { actionTransformer });

console.log(store.dispatch(createAction('INCREMENT'))); // { type: 'INCREMENT', transformed: true }

Contribution

To release:

npm run release

To see other available commands:

npm run

License

MIT

版本与发布记录

@bigcommerce/data-store 共发布 14 个版本,首次发布于 2018/05/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2025/09/051.2 年前
1.0.22024/01/232 年前
1.0.12019/08/077 年前
1.0.02019/08/057 年前
0.2.72019/05/237 年前
0.2.62019/01/087 年前

关于 @bigcommerce/data-store 的常见问题

@bigcommerce/data-store 是什么?

A JavaScript library for managing application state

如何安装 @bigcommerce/data-store?

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

@bigcommerce/data-store 支持 TypeScript 吗?

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

@bigcommerce/data-store 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bigcommerce/data-store 可以商用吗?

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

@bigcommerce/data-store 的下载量有多少?

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

@bigcommerce/data-store 还在维护吗?

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