onyx
できること
Onyxがアプリのデータを保存し、リアクティブに更新し、オフラインで優先される変更をどう扱うかを説明します。Onyxライブラリでアプリを作る開発者が対象です。
使う場面
- ✓useOnyxフックを使うか、コンポーネントをOnyxのデータに接続するとき。
- ✓Onyxの状態を変更するアクションファイルを書くとき。
- ✓オフラインでも実行されるかもしれないAPI呼び出しに、楽観的な更新を加えるとき。
スキルのリンク
https://github.com/Expensify/App/blob/main/.claude/skills/onyx/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“ユーザーのプロフィールをOnyxに保存し、その変更をすぐに表示するアクションファイルを書く手伝いをしてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
エージェントはアクションファイルの書き方に従い、状態の変更はアクションファイルでのみ行います。その後、APIのリクエストが終わる前に画面が更新されるよう、楽観的なデータを加えます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Core Concepts
Onyx is a persistent storage solution wrapped in a Pub/Sub library that enables reactive, offline-first data management — key-value storage with automatic AsyncStorage persistence, reactive subscriptions, and collection management.
For the full API reference (initialization, storage providers, cache eviction, benchmarks, Redux DevTools), see https://github.com/Expensify/react-native-onyx/blob/main/README.md.
Common Patterns
Action File Pattern
IMPORTANT: Onyx state must only be modified from action files (src/libs/actions/). Never call Onyx.merge, Onyx.set, Onyx.clear, or API.write directly from a component.
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
function setIsOffline(isNetworkOffline: boolean, reason = '') {
if (reason) {
Log.info(`[Network] Client is ${isNetworkOffline ? 'offline' : 'online'} because: ${reason}`);
}
Onyx.merge(ONYXKEYS.NETWORK, {isOffline: isNetworkOffline});
}
export {setIsOffline};
Optimistic Updates Pattern
Optimistic updates allow users to see changes immediately while the API request is queued. This is fundamental to Expensify's offline-first architecture.
For which pattern to use (A / B / C / D) and UX behavior for each, see https://github.com/Expensify/App/blob/main/contributingGuides/philosophies/OFFLINE.md.
Understanding the Three Data Sets
CRITICAL: Backend response data is automatically applied via Pusher updates or HTTPS responses. You do NOT manually set backend data in successData/failureData — only UI state cleanup goes there.
optimisticData (Applied immediately, before the API call)
- Mirrors what the backend would return on success
- Gives the user instant feedback without waiting for the server
- Often includes
pendingActionto flag the change as in-flight (e.g. greying out a comment while offline) pendingActionis cleared oncesuccessDataorfailureDatais applied
successData (Applied when API succeeds)
- Used for UI state cleanup: clearing
pendingAction, settingisLoading: false - For
addactions: often not needed (optimisticData already set the right state) - For
update/deleteactions: include to clear pending state
- Used for UI state cleanup: clearing
failureData (Applied when API fails)
- Reverts optimisticData changes
- Clears
pendingAction. - Adds
errorsfield for the user to see - Always include this to handle unexpected failures
For code examples of each pattern (A/B, loading state, finallyData), see offline-patterns.md.
Performance Optimization
1. Subscribe to Specific Collection Members
// BAD: re-renders on any report change
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const myReport = allReports[`report_${reportID}`];
// GOOD: re-renders only when this report changes
const [myReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
2. Use Selectors to Narrow Re-renders
const accountIDSelector = (account: Account) => account?.accountID;
const [accountID] = useOnyx(ONYXKEYS.ACCOUNT, {selector: accountIDSelector});
useOnyx caches by selector reference — a new function reference on every render bypasses the cache and causes unnecessary re-renders. Prefer pure selectors defined in src/selectors/ over inline functions. If a selector must be defined inside a component, ensure referential stability: React Compiler handles this automatically, but in components that are not compiled, wrap the selector in useMemo.
// BAD: new function reference on every render defeats caching
const [accountID] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account) => account?.accountID});
// GOOD: stable reference defined outside the component
// src/selectors/accountSelectors.ts
const selectAccountID = (account: Account) => account?.accountID;
// GOOD: stable reference via useMemo (for non-React-Compiler components)
const selector = useMemo(() => (account: Account) => account?.accountID, []);
const [accountID] = useOnyx(ONYXKEYS.ACCOUNT, {selector});
For skipCacheCheck (large objects) and batch collection update patterns, see https://github.com/Expensify/react-native-onyx/blob/main/README.md.
Common Pitfalls
Mixing set and merge on the Same Key
Onyx.set() calls are not batched with Onyx.merge() calls, which can produce race conditions:
// BAD: merge may
よくある質問
コンポーネントがOnyxの状態を直接変更できますか?
いいえ。スキルは、Onyxの状態はアクションファイルからだけ変更するよう定めています。
楽観的な更新とは何ですか?
APIのリクエストがまだキューに入っている間に、利用者に対してすぐに表示される変更です。
Onyx APIの完全なリファレンスはどこにありますか?
スキルは、完全なリファレンスとして、Onyxライブラリ のREADMEを案内しています。
詳細
作者による元の説明
Onyx state management patterns — useOnyx hook, action files, optimistic updates, collections, and offline-first architecture. Use when working with Onyx connections, writing action files, debugging state, or implementing API calls with optimistic data.
repo: Expensify/App
人気
- GitHubのスター
- 5.1k
- ランキング
- #15049
- ライセンス
- MIT
- 更新日
- 2026/11/2
コメント
- …
似ているスキル
angular-developer
280kエージェントがAngularのコードを作成し、プロジェクト構成についてアドバイスできるようにします。新しいプロジェクトの開始、コード部品の作成、パフォーマンスやアクセシビリティの改善に役立ちます。
frontend-a11y
280kエージェントが Web インターフェースを誰にとっても使いやすくするよう支援します。スクリーンリーダーやキーボード操作に対応し、サイトやアプリが適切に動作するようにします。
frontend-patterns
280kエージェントがフロントエンド開発の最新パターンを適用できるようにします。React と Next.js において、パフォーマンスの最適化、状態管理、ベストプラクティスの遵守を行います。