본문으로 건너뛰기
Skillpedia
웹사이트 및 UI (프런트엔드)5.1k · #15049

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가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/Expensify/App/blob/main/.claude/skills/onyx/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“사용자 프로필을 Onyx에 저장하고 변경 사항을 바로 보여 주는 액션 파일을 작성하도록 도와주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

에이전트가 액션 파일 패턴을 따라 상태 변경은 액션 파일에서만 일어나도록 하고, 요청이 끝나기 전에 화면이 갱신되도록 낙관적 데이터를 추가합니다.

기술 내용 (원본 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 참고 자료는 어디에 있나요?

스킬은 전체 참고 자료로 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.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

비슷한 스킬