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.
66
80%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
Fix and improve this skill with Tessl
tessl review fix ./.claude/skills/onyx/SKILL.mdOnyx 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.
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 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.
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)
pendingAction to flag the change as in-flight (e.g. greying out a comment while offline)pendingAction is cleared once successData or failureData is appliedsuccessData (Applied when API succeeds)
pendingAction, setting isLoading: falseadd actions: often not needed (optimisticData already set the right state)update/delete actions: include to clear pending statefailureData (Applied when API fails)
pendingAction.errors field for the user to seeFor code examples of each pattern (A/B, loading state, finallyData), see offline-patterns.md.
// 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}`);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.
Onyx.set() calls are not batched with Onyx.merge() calls, which can produce race conditions:
// BAD: merge may execute before set resolves
Onyx.set(ONYXKEYS.ACCOUNT, null);
Onyx.merge(ONYXKEYS.ACCOUNT, {validated: true});
// GOOD: use one operation
Onyx.set(ONYXKEYS.ACCOUNT, {validated: true});// Update a single field
Onyx.merge(ONYXKEYS.NETWORK, {isOffline: true});
// Delete data
Onyx.set(ONYXKEYS.ACCOUNT, null);
// Subscribe in component
const [data] = useOnyx(ONYXKEYS.SOME_KEY);
// Subscribe with selector
const [field] = useOnyx(ONYXKEYS.SOME_KEY, {selector: (data) => data?.specificField});
// Update collection member
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {unread: false});
// Batch update collection
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, updates);
// API call with optimistic update
API.write('SomeCommand', params, {optimisticData, successData, failureData});0498f1e
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.