globostate

中文文档
0.5.0发布于 2026/11/04MITTypeScriptES Module零依赖构建来源已验证

A path-based state container for React

原始使用文档
文档目录9 节
  1. Getting Started
  2. The Provider
  3. The Hook
  4. Updater functions
  5. Paths
  6. Re-renders
  7. Separate stores
  8. Requirements
  9. Putting it all together

Globostate

Globostate is a React hook that creates a global, immutable state and allows you to read and update any part of it by using a path syntax similar to lodash's _.get and _.set functions.

Here's what it looks like:

const ExampleComponent = ()  => {
  const [count, setCount] = useGloboState('app.components.counterValue', 0)
  return (
    <main>
      <p>You clicked {count} times.</p>
      <button onClick={() => setCount(count + 1)}>Click Me</button>
    </main>
  )
}

Getting Started

Globostate requires React as a peer dependency. To get started:

npm i react react-dom globostate

The Provider

Globostate includes a Provider component that must wrap your application:

import React from 'react'
import { createRoot } from 'react-dom/client'
import { Provider, useGloboState } from 'globostate'

const container = document.getElementById('app')
const root = createRoot(container)

root.render(
  <Provider>
    <App />
  </Provider>
)

The Hook

The useGloboState hook works very similarly to React's useState hook but global state can be accessed anywhere in the application without drilling down state through props.

useGloboState accepts two parameters. The first is the object path, written with dots and bracket indexes ('lists.todos[0].title') or as an array of keys (['lists', 'todos', 0, 'title']). Updates are immutable: only the objects along that path are copied. The second is an initial state, similar to useState's only parameter.

const [todos, setTodos] = useGloboState('appState.lists.todos', [])

The default is returned whenever nothing is stored at the path yet. It isn't written into the store until you call the setter.

Updater functions

Like useState, the setter also accepts a function. It receives the current value (or the default, if nothing is stored yet) and returns the next one:

const [count, setCount] = useGloboState('app.count', 0)
setCount(c => c + 1)

Because a function passed to the setter is always treated as an updater, store a function by wrapping it: setHandler(() => myFunction).

Paths

  • Dots and brackets: 'lists.todos[0].title' and 'lists.todos.0.title' are the same path.
  • Arrays of keys: ['lists', 'todos', 0, 'title']. Use this form when a key contains a dot or a bracket.
  • Creating missing levels: when a set goes through a level that doesn't exist yet, globostate creates an array if the next key is a non-negative integer (0, 12), and a plain object otherwise.
  • Not supported: quoted keys such as a["b"].

Re-renders

A component re-renders only when the value at its path changes. Arrays and plain objects are compared shallowly, so setting an equal copy doesn't re-render. Everything else, including Dates, Maps, Sets and class instances, is compared by reference, so replace these values rather than mutating them.

Separate stores

Provider and useGloboState share one store. To create an independent store, for example for a self-contained widget or library, call createProvider:

import { createProvider } from 'globostate'

const { Provider: WidgetProvider, useGloboState: useWidgetState } = createProvider()

Requirements

  • React 18 or newer.
  • globostate is published as an ES module. import works everywhere. Loading it with require() needs Node 20.19+ or 22.12+.

Putting it all together

Here is what a full app might look like:

import React from 'react'
import { createRoot } from 'react-dom/client'
import { Provider, useGloboState } from 'globostate'

const App = () => {
  const [value, updateValue] = useGloboState('app.counterValue', 0)
  return (
    <main>
      <div><button onClick={() => updateValue(value - 1)}>-</button></div>
      <div>{value}</div>
      <div><button onClick={() => updateValue(value + 1)}>+</button></div>
    </main>
  )
}

const container = document.getElementById('app')
const root = createRoot(container)

root.render(
  <Provider>
    <App />
  </Provider>
)

版本与发布记录

globostate 共发布 7 个版本,首次发布于 2024/01/06;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.0latest2026/11/04今天
0.4.22025/10/161 年前
0.4.12025/09/291.1 年前
0.4.02025/09/291.1 年前
0.3.02024/05/182 年前
0.2.02024/04/092 年前

关于 globostate 的常见问题

globostate 是什么?

React的基于路径的状态容器

如何安装 globostate?

在项目目录运行 npm install globostate,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

globostate 支持 TypeScript 吗?

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

globostate 支持 ESM 还是 CommonJS?

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

globostate 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

globostate 可以商用吗?

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

globostate 还在维护吗?

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