valtio-define

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Based on valtio-define v1.0.1. A Valtio-based state management library with Pinia-like API for React applications.
基于valtio-define v1.0.1。这是一个基于Valtio的状态管理库,为React应用提供类Pinia风格的API。

Overview

概述

valtio-define provides a factory function for creating reactive stores with state, actions, and getters. Built on top of Valtio, it offers a familiar API similar to Pinia with full TypeScript support.
valtio-define提供一个工厂函数,用于创建包含state、actions和getters的响应式store。它构建在Valtio之上,提供类似Pinia的熟悉API,并完全支持TypeScript。

Core References

核心参考

TopicDescriptionReference
defineStoreCore API for creating reactive storescore-define-store
useStoreReact hook for accessing store statecore-use-store
TypesTypeScript types and interfacescore-types
主题描述参考文档
defineStore创建响应式store的核心APIcore-define-store
useStore用于访问store状态的React hookcore-use-store
TypesTypeScript类型和接口core-types

Advanced Features

高级功能

TopicDescriptionReference
SubscriptionsSubscribe to state changesadvanced-subscribe
PatchUpdate state with patch operationsadvanced-patch
SignalJSX component for inline reactive valuesadvanced-signal
Store to StateConvert store to useState-like hooksadvanced-store-to-state
主题描述参考文档
Subscriptions订阅状态变更advanced-subscribe
Patch使用patch操作更新状态advanced-patch
Signal用于内联响应式值的JSX组件advanced-signal
Store to State将store转换为类似useState的hooksadvanced-store-to-state

Features

功能特性

TopicDescriptionReference
Plugin SystemExtend stores with pluginsfeature-plugin-system
Persistent PluginPersist state to storagefeature-persistent-plugin
主题描述参考文档
Plugin System使用插件扩展storefeature-plugin-system
Persistent Plugin将状态持久化到存储中feature-persistent-plugin

Quick Start

快速开始

tsx
import { defineStore, useStore } from 'valtio-define'

const store = defineStore({
  state: () => ({ count: 0 }),
  actions: {
    increment() { this.count++ },
  },
  getters: {
    doubled() { return this.count * 2 },
  },
})

function Counter() {
  const state = useStore(store)
  return (
    <div>
      <div>Count: {state.count}</div>
      <div>Doubled: {state.doubled}</div>
      <button onClick={store.increment}>Increment</button>
    </div>
  )
}
tsx
import { defineStore, useStore } from 'valtio-define'

const store = defineStore({
  state: () => ({ count: 0 }),
  actions: {
    increment() { this.count++ },
  },
  getters: {
    doubled() { return this.count * 2 },
  },
})

function Counter() {
  const state = useStore(store)
  return (
    <div>
      <div>Count: {state.count}</div>
      <div>Doubled: {state.doubled}</div>
      <button onClick={store.increment}>Increment</button>
    </div>
  )
}