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/jotai.md.
  • 简体中文
  • Jotai

    使用 jotai

    Jotai 采用“原子化”的方法来管理全局 React 状态。

    安装依赖

    npm
    yarn
    pnpm
    bun
    deno
    npm install jotai

    示例

    import { useEffect } from '@lynx-js/react';
    import { atom, useAtom } from 'jotai';
    
    const counter = atom(0);
    
    export function App() {
      const [count, setCounter] = useAtom(counter);
    
      useEffect(() => {
        console.log('count changed:', count);
      }, [count]);
    
      return (
        <view>
          <text>{count}</text>
          <text bindtap={() => setCounter((prev) => prev + 1)}>Tap</text>
        </view>
      );
    }

    更多细节请参考 Jotai - atom

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