For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/react/state-management/valtio.md.
  • 简体中文
  • Valtio

    使用 valtio

    Valtio 是一个轻量灵活的状态管理库,提供细粒度订阅和响应式更新。

    安装依赖

    npm
    yarn
    pnpm
    bun
    deno
    npm install valtio

    示例

    import { useEffect } from '@lynx-js/react';
    import { proxy, useSnapshot, subscribe } from 'valtio';
    
    const state = proxy<{ count: number }>({ count: 0 });
    
    export function App() {
      const snap = useSnapshot(state);
    
      const handleTap = () => {
        state.count++;
      };
    
      useEffect(() => {
        const unsubscribe = subscribe(state, () => {
          console.log('state changed: ', state.count);
        });
    
        return () => {
          unsubscribe();
        };
      }, []);
    
      return (
        <view>
          <text>{snap.count}</text>
          <text bindtap={handleTap}>Tap</text>
        </view>
      );
    }

    更多细节请参考 valtio - basic

    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。