1k state management

From OneKeyHQ/app-monorepo · View interactive version

1k state management is an AI agent skill from the GitHub repository OneKeyHQ/app-monorepo. Jotai state management patterns — atoms, globalAtom, contextAtom, and persistence.

How to use this skill

Skill file

OneKey State Management

Jotai Atom Organization - MANDATORY STRUCTURE

Global State Atoms (for app-wide, persistent state)

Feature-Specific State Atoms (for localized functionality)

```

contexts/

├── marketV2/

│ ├── atoms.ts - State definitions

│ ├── actions.ts - State operations

│ └── index.ts - Exports

├── swap/

│ ├── atoms.ts

│ ├── actions.ts

│ └── index.ts

```

FORBIDDEN Atom Patterns

Atom Selection Guidelines

Use globalAtom when:

Use contextAtom when:

IMPORTANT: These are the ONLY two atom patterns used in the project. Do not create custom atom patterns or use plain Jotai atoms outside of these established structures.

Common Patterns

Creating a Global Atom

// packages/kit-bg/src/states/jotai/atoms/myFeature.ts
import { globalAtom } from '../utils';
import { EAtomNames } from '../atomNames';

export const myFeatureAtom = globalAtom<MyFeatureState>({
  name: EAtomNames.myFeature,
  initialValue: { /* initial state */ },
  persist: true, // if persistence needed
});

Creating a Context Atom

// packages/kit/src/states/jotai/contexts/myFeature/atoms.ts
import { createJotaiContext } from '../../utils';

const { contextAtom, useContextAtom } = createJotaiContext();

export const myFeatureDataAtom = contextAtom<MyData | null>(null);

// Export hook for components
export { useContextAtom };

Using Atoms in Components

import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { myFeatureAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms';

function MyComponent() {
  // Read and write
  const [value, setValue] = useAtom(myFeatureAtom);

  // Read only
  const value = useAtomValue(myFeatureAtom);

  // Write only
  const setValue = useSetAtom(myFeatureAtom);
}
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.