Skip to content

状态管理完整索引

Source: src/stores/ | src/queries/

概览

应用使用 TanStack Store 管理客户端状态,TanStack Query 管理服务端状态。


技术栈

版本用途
@tanstack/react-store0.7.x客户端状态 (同步)
@tanstack/react-query5.x服务端状态 (异步)

Stores (客户端状态)

Store文件行数职责
walletStorewallet.ts788钱包管理、地址、代币
chainConfigStorechain-config.ts182链配置
preferencesStorepreferences.ts~100用户偏好
addressBookStoreaddress-book.ts~150地址簿
notificationStorenotification.ts~80通知
ecosystemStoreecosystem.ts~100小程序生态
securityPasswordStoresecurity-password.ts~60安全密码

Queries (服务端状态)

Query文件职责
useBalanceQueryuse-balance-query.ts余额查询
useTransactionHistoryQueryuse-transaction-history-query.ts交易历史
usePriceQueryuse-price-query.ts价格查询
useExchangeRateQueryuse-exchange-rate-query.ts汇率查询
useStakingQueryuse-staking-query.ts质押信息
useAddressTransactionsQueryuse-address-transactions-query.ts地址交易
useAddressBalanceQueryuse-address-balance-query.ts地址余额
useChainConfigQueryuse-chain-config-query.ts链配置
useSecurityPasswordQueryuse-security-password-query.ts安全密码

架构图

┌─────────────────────────────────────────────────────────────────────────┐
│                          React Components                               │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌───────────────┐  │
│  │   Pages     │  │    Sheets   │  │    Tabs     │  │   Components  │  │
│  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘  └───────┬───────┘  │
└─────────┼───────────────┼───────────────┼───────────────────┼──────────┘
          │               │               │                   │
          ▼               ▼               ▼                   ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                              Hooks Layer                                │
│  ┌─────────────────────────────────┐ ┌────────────────────────────────┐│
│  │         useStore()              │ │         useQuery()             ││
│  │   (TanStack Store Hooks)        │ │     (TanStack Query Hooks)     ││
│  └────────────────┬────────────────┘ └───────────────┬────────────────┘│
└───────────────────┼──────────────────────────────────┼─────────────────┘
                    │                                  │
                    ▼                                  ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                           State Layer                                   │
│  ┌─────────────────────────────────┐ ┌────────────────────────────────┐│
│  │           Stores                │ │           Queries              ││
│  │  ┌─────────┐ ┌─────────────┐   │ │  ┌─────────┐ ┌─────────────┐   ││
│  │  │ wallet  │ │ chainConfig │   │ │  │ balance │ │   price     │   ││
│  │  └─────────┘ └─────────────┘   │ │  └─────────┘ └─────────────┘   ││
│  │  ┌─────────┐ ┌─────────────┐   │ │  ┌─────────┐ ┌─────────────┐   ││
│  │  │  prefs  │ │ addressBook │   │ │  │ history │ │  staking    │   ││
│  │  └─────────┘ └─────────────┘   │ │  └─────────┘ └─────────────┘   ││
│  └────────────────┬────────────────┘ └───────────────┬────────────────┘│
└───────────────────┼──────────────────────────────────┼─────────────────┘
                    │                                  │
                    ▼                                  ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                          Service Layer                                  │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐│
│  │wallet-storage│ │chain-config │ │chain-adapter│ │  currency-exchange ││
│  └─────────────┘ └─────────────┘ └─────────────┘ └─────────────────────┘│
└─────────────────────────────────────────────────────────────────────────┘

Store vs Query 选择指南

场景选择原因
当前选中的钱包Store纯客户端状态,需要同步更新
链配置列表Store本地持久化,偶尔远程刷新
用户偏好设置Store本地存储
代币余额Query需要定时刷新,有缓存策略
交易历史Query分页加载,需要失效策略
法币汇率Query外部 API,有缓存

文件结构

src/
├── stores/
│   ├── index.ts              # 统一导出
│   ├── hooks.ts              # 共享 hooks
│   ├── wallet.ts             # 钱包 store (788 行)
│   ├── chain-config.ts       # 链配置 store
│   ├── preferences.ts        # 偏好 store
│   ├── address-book.ts       # 地址簿 store
│   ├── notification.ts       # 通知 store
│   ├── ecosystem.ts          # 生态 store
│   ├── security-password.ts  # 安全密码 store
│   └── currencies.ts         # 货币 store

└── queries/
    ├── index.ts                      # 统一导出
    ├── use-balance-query.ts          # 余额
    ├── use-price-query.ts            # 价格
    ├── use-exchange-rate-query.ts    # 汇率
    ├── use-transaction-history-query.ts  # 交易历史
    ├── use-address-transactions-query.ts # 地址交易
    ├── use-address-balance-query.ts  # 地址余额
    ├── use-chain-config-query.ts     # 链配置
    ├── use-staking-query.ts          # 质押
    └── use-security-password-query.ts # 安全密码

使用示例

Store 使用

typescript
import { useStore } from '@tanstack/react-store';
import { walletStore, walletActions } from '@/stores';

function WalletPage() {
  // 订阅整个 store
  const state = useStore(walletStore);
  
  // 订阅部分状态 (优化渲染)
  const currentWalletId = useStore(walletStore, s => s.currentWalletId);
  const wallets = useStore(walletStore, s => s.wallets);
  
  // 触发 action
  const handleSwitch = (walletId: string) => {
    walletActions.setCurrentWallet(walletId);
  };
}

Query 使用

typescript
import { useBalanceQuery, useRefreshBalance } from '@/queries';

function BalanceDisplay({ walletId, chain }: Props) {
  const { data: tokens, isLoading, error } = useBalanceQuery(walletId, chain);
  const { refresh } = useRefreshBalance();
  
  // 下拉刷新
  const handleRefresh = () => refresh(walletId, chain);
  
  if (isLoading) return <Skeleton />;
  if (error) return <Error message={error.message} />;
  
  return <TokenList tokens={tokens} onRefresh={handleRefresh} />;
}

相关文档

Released under the MIT License.