Skill站外收录 @skilllib/skill-2014279369244487682
1k-state-management
统一管理应用全局与功能级状态,通过标准化原子结构实现跨模块数据共享和持久化存储,确保状态在多场景下的一致性与可维护性。
软件开发 AI 技能
Skills.MD
# OneKey State Management
## Jotai Atom Organization - MANDATORY STRUCTURE
### Global State Atoms (for app-wide, persistent state)
- **Location**: `packages/kit-bg/src/states/jotai/atoms/`
- **Usage**: Global settings, account state, hardware state, currency, etc.
- **Pattern**: Use `globalAtom` and `EAtomNames` for standardization
- **Examples**: `settings.ts`, `account.ts`, `hardware.ts`, `currency.ts`
### Feature-Specific State Atoms (for localized functionality)
- **Location**: `packages/kit/src/states/jotai/contexts/[feature_name]/atoms.ts`
- **Usage**: Feature-specific state that may be shared across components within that feature
- **Pattern**: Use `contextAtom` from `createJotaiContext` for consistency
- **Structure**:
```
contexts/
├── marketV2/
│ ├── atoms.ts - State definitions
│ ├── actions.ts - State operations
│ └── index.ts - Exports
├── swap/
│ ├── atoms.ts
│ ├── actions.ts
│ └── index.ts
```
## FORBIDDEN Atom Patterns
- ❌ **NEVER** create atom directories under `packages/kit/src/views/`
- ❌ **NEVER** create standalone atom files in component directories
- ❌ **NEVER** mix `globalAtom` and `contextAtom` patterns without architectural justification
## Atom Selection Guidelines
### Use globalAtom when:
- State needs persistence across app restarts
- State is used across multiple major features
- State affects the entire application (settings, authentication, etc.)
- Located in `packages/kit-bg/src/states/jotai/atoms/`
### Use contextAtom when:
- State is specific to a feature or module
- State is temporary/session-based
- State is shared within related components of a feature
- Located in `packages/kit/src/states/jotai/contexts/[name]/atoms.ts`
**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
```typescript
// 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
```typescript
// 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
```typescript
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);
}
```
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「1k-state-management」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次