> 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.

# Valtio

## 使用 `valtio`

[Valtio](https://valtio.dev/) 是一个轻量灵活的状态管理库，提供细粒度订阅和响应式更新。

### 安装依赖


```sh [npm]
npm install valtio
```

```sh [yarn]
yarn add valtio
```

```sh [pnpm]
pnpm add valtio
```

```sh [bun]
bun add valtio
```

```sh [deno]
deno add npm:valtio
```

### 示例

```tsx
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](https://valtio.dev/docs/api/basic/proxy)
