Skillpedia
المواقع والواجهات (frontend)5.1k · #15049

onyx

ما الذي تفعله

توضح هذه المهارة كيفية تخزين Onyx لبيانات التطبيق وتحديثها تفاعليًا والتعامل مع التغييرات دون اتصال أولًا. وهي موجهة للمطورين الذين يبنون تطبيقات بمكتبة Onyx.

متى تستخدمها

  • ✓عندما تستخدم خطاف useOnyx أو توصل مكونًا ببيانات Onyx.
  • ✓عندما تكتب ملفات إجراءات تغيّر حالة Onyx.
  • ✓عندما تضيف تحديثات متفائلة لاستدعاء API قد يعمل دون اتصال.

رابط المهارة

https://github.com/Expensify/App/blob/main/.claude/skills/onyx/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/Expensify/App/blob/main/.claude/skills/onyx/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“ساعدني في كتابة ملف إجراء يحفظ ملف المستخدم الشخصي في Onyx ويعرض التغيير فورًا.”

5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.

كيف تعمل

يتبع الوكيل نمط ملف الإجراء، بحيث تحدث تغييرات الحالة في ملفات الإجراءات فقط، ثم يضيف بيانات متفائلة حتى تتحدث الشاشة قبل انتهاء الطلب.

المحتوى التقني (ملف 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.

  1. 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 pendingAction to flag the change as in-flight (e.g. greying out a comment while offline)
    • pendingAction is cleared once successData or failureData is applied
  2. successData (Applied when API succeeds)

    • Used for UI state cleanup: clearing pendingAction, setting isLoading: false
    • For add actions: often not needed (optimisticData already set the right state)
    • For update/delete actions: include to clear pending state
  3. failureData (Applied when API fails)

    • Reverts optimisticData changes
    • Clears pendingAction.
    • Adds errors field 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؟

تشير المهارة إلى README مكتبة Onyx للمرجع الكامل.

التفاصيل

الوصف الأصلي من المؤلف

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
آخر تحديث
2‏/11‏/2026

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

مهارات مشابهة