Skip to content

[Due for payment 2026-09-10] Remove OnyxUtils.get() from OnyxDerived init (use connectWithoutView) #98607

Closed

Description

@tgolen

Problem

OnyxUtils.get() (imported from the internal deep path react-native-onyx/dist/OnyxUtils) is not a sanctioned way to read Onyx data. The only two supported read APIs are:

  1. useOnyx() — for anything in a component's render path.
  2. Onyx.connectWithoutView() — for non-render logic that genuinely can't use useOnyx.

Reaching into react-native-onyx/dist/* bypasses the public API and couples us to Onyx internals.

This issue covers src/libs/actions/OnyxDerived/index.ts, which contains one OnyxUtils.get(key) call (as of writing, line 46).

Why connectWithoutView (not useOnyx) here

This is module-level init() logic, not a component — useOnyx() is not applicable. The call performs a one-time hydration read of the stored derived value from disk before wiring up the dependency subscriptions. The sanctioned replacement is a short-lived connectWithoutView read: connect, read the first callback value, then Onyx.disconnect(connection).

Because the connection is short-lived (disconnected after the first callback), it will not recursively re-fire when this same code later writes back to the derived key.

Reference implementation of the short-lived pattern already in the codebase: src/libs/migrations/ConvertGpsPointsTo2DArray.ts.

Proposed change

  • Remove the import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'.
  • Replace OnyxUtils.get(key).then((storedDerivedValue) => { ... }) with a short-lived Onyx.connectWithoutView read that resolves the stored value, then continues into the existing setup logic.
  • Keep all downstream behavior (the dependencyValues wiring, connectWithoutView dependency subscriptions, flush/coalesce logic) unchanged.

Acceptance criteria

  • No references to OnyxUtils / react-native-onyx/dist/OnyxUtils remain in this file.
  • The initial derived-value hydration uses Onyx.connectWithoutView (short-lived) via the public Onyx import.
  • Derived values still restore from disk on init and recompute correctly when dependencies change.

Notes

This is one of several issues removing OnyxUtils.get() across the app. A separate issue will add an ESLint rule to prevent it from being reintroduced.

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @mallenexpensify

Activity

  1. added
    External
    Daily
    Improvement
    on Aug 13, 2026
  2. melvin-bot commented on Aug 13, 2026

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @Ollyws (External)

  3. melvin-bot commented on Aug 13, 2026

    @melvin-bot
  4. self-assigned this
    on Aug 13, 2026
  5. skrohan5016-coder commented on Aug 13, 2026

    @skrohan5016-coder

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    src/libs/actions/OnyxDerived/index.ts performs a one-time startup read with OnyxUtils.get(key), imported from the internal deep path react-native-onyx/dist/OnyxUtils. Runtime code should use the public Onyx API instead, while preserving the current initialization behavior: restore the persisted derived value first, then establish the long-lived dependency subscriptions.

    What is the root cause of that problem?

    OnyxDerived.init() needs a one-shot, non-React read for every derived key. Since this code runs at module startup outside the React lifecycle, useOnyx() is not applicable, and the existing implementation reached into OnyxUtils.get() as a promise-based shortcut.

    The important detail is the ordering around that read. The current .then(...) continuation contains all dependency wiring, so no dependency can complete the initial set and recompute until the persisted derived value has been assigned to derivedValue. Moving those subscriptions outside the hydration callback would introduce a startup race: the first recompute could run with context.currentValue still undefined and overwrite a valid disk-cached derived value.

    The replacement must also disconnect after the first callback. Leaving the derived-key connection active would make later writes to that same derived key re-enter the initialization block and install another set of dependency listeners.

    What changes do you think we should make in order to solve the problem?

    1. Remove the runtime import of react-native-onyx/dist/OnyxUtils from src/libs/actions/OnyxDerived/index.ts.
    2. Replace OnyxUtils.get(key).then(...) with a short-lived Onyx.connectWithoutView({key, callback}) read.
    3. In the first callback, immediately call Onyx.disconnect(connection) before continuing with initialization.
    4. Keep the existing initialization body—including derivedValue restoration, dependency arrays, long-lived dependency subscriptions, coalescing, clear handling, and self-healing logic—inside that one-shot callback. This preserves the existing hydrate-before-subscribe ordering.
    5. Follow the established pattern already used by src/libs/migrations/ConvertGpsPointsTo2DArray.ts. The default connection reuse behavior is sufficient: disconnecting this returned connection removes this callback without tearing down other consumers of the same key.

    Conceptually:

    const connection = Onyx.connectWithoutView({
        key,
        callback: (storedDerivedValue) => {
            Onyx.disconnect(connection);
    
            let derivedValue = storedDerivedValue;
            // Existing initialization and dependency wiring remains here unchanged.
        },
    });

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    Add focused coverage to the existing OnyxDerived tests for these invariants:

    1. Seed a persisted derived value before initializing OnyxDerived and verify that the first compute receives it as context.currentValue, proving hydration still occurs before dependency-driven recomputation.
    2. Verify the hydration subscription is one-shot: writing the derived key after initialization must not create another set of dependency subscriptions or cause duplicate recomputations.
    3. Keep the existing OnyxDerived and self-healing suites passing to confirm normal recomputation, microtask coalescing, Onyx.clear() handling, and failed-compute recovery are unchanged.

    What alternative solutions did you explore? (Optional)

    • useOnyx(): not usable because this is non-React startup logic.
    • A permanent connectWithoutView() subscription: rejected because writes to the derived key could re-run initialization and duplicate listeners.
    • Starting dependency subscriptions before hydration completes: rejected because it changes the current ordering and can overwrite the restored value.
    • Adding a new public promise-based getter to react-native-onyx: unnecessary scope for a one-shot read already supported by connectWithoutView().
  6. tgolen commented on Aug 17, 2026

    @tgolen
    ContributorAuthor

    @Ollyws Are you OK with that proposal?

  7. nkdengineer commented on Aug 19, 2026

    @nkdengineer
    Contributor

    Proposal

    What is the root cause of that problem?

    src/libs/actions/OnyxDerived/index.ts restores the stored derived value with OnyxUtils.get(key), imported from the internal deep path react-native-onyx/dist/OnyxUtils. That is not a sanctioned Onyx read API and couples the derived-value engine to Onyx internals.

    This code is not in a React render path, so useOnyx() is not applicable. The read happens during module-level init() before the dependency subscriptions are established, purely to hydrate the last stored derived value from disk and seed derivedValue before recomputes begin. That makes this a non-render, one-shot read, which is exactly the case Onyx.connectWithoutView() is meant to cover.

    The subtle requirement is preserving the current bootstrap ordering: restore storedDerivedValue, then continue into the existing dependency wiring, coalescing, and flush logic unchanged. A short-lived connectWithoutView fits that pattern because it can read the first callback value, then immediately disconnect before the later setDerivedValue() writes occur.

    What changes do you think we should make in order to solve the problem?

    Replace the internal OnyxUtils.get() hydration read with the same short-lived Onyx.connectWithoutView() pattern already used by ConvertGpsPointsTo2DArray.ts.

    Concretely:

    1. Remove import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'.
    2. Replace OnyxUtils.get(key).then((storedDerivedValue) => { ... }) with a small promise wrapper around Onyx.connectWithoutView:
      • connect to key
      • capture the first callback value as storedDerivedValue
      • immediately Onyx.disconnect(connection)
      • continue into the existing setup logic
    3. Keep the rest of init() unchanged: restoring derivedValue, establishing dependency subscriptions, deferring writes until all connections initialize, and preserving the current clear/reset and microtask coalescing behavior.

    This keeps the behavior the same while moving the initial hydration read onto the public Onyx API.

    What alternative solutions did you explore? (Optional)

    1. Use useOnyx() instead: rejected because this is module-level initialization logic, not a component or hook render path.
    2. Skip the stored-value hydration and always recompute from scratch: rejected because the current code intentionally restores the disk-cached derived value first, which avoids unnecessary churn during startup and preserves the existing bootstrap behavior.
  8. Ollyws commented on Aug 19, 2026

    @Ollyws
    Contributor

    @MelvinBot create a draft PR from the proposal outlined in #98607 (comment)

  9. MelvinBot commented on Aug 19, 2026

    @MelvinBot
    Contributor

    🤖 Draft PR created: #99004

    I implemented the proposal from this issue — replacing the internal OnyxUtils.get(key) hydration read in src/libs/actions/OnyxDerived/index.ts with a short-lived Onyx.connectWithoutView read (disconnected after the first callback), following the ConvertGpsPointsTo2DArray.ts pattern.

    Acceptance criteria:

    • ✅ No references to OnyxUtils / react-native-onyx/dist/OnyxUtils remain in the file (import removed).
    • ✅ Initial derived-value hydration now uses Onyx.connectWithoutView (short-lived) via the public Onyx import.
    • ✅ Hydrate-before-subscribe ordering and all downstream dependency wiring / flush / coalesce logic are unchanged.

    Checks run locally (all pass): typecheck (tsgo), lint, and the OnyxDerived unit suites — OnyxDerivedTest, OnyxDerivedSelfHealTest, CardFeedErrorsDerivedValueTest (69 tests).

    Please mark the PR as "Ready for review" when it is ready.


    view run

  10. 12 remaining items

  11. melvin-bot commented on Sep 8, 2026

    @melvin-bot

    @Ollyws Huh... This is 4 days overdue. Who can take care of this?

  12. added
    Awaiting Payment
    on Sep 9, 2026
  13. melvin-bot commented on Sep 9, 2026

    @melvin-bot

    Triggered auto assignment to @mallenexpensify (Awaiting Payment)

  14. melvin-bot commented on Sep 9, 2026

    @melvin-bot

    Payment Summary

    Resolving PRs:

    BugZero Checklist (@mallenexpensify)

    • I have confirmed assignees, roles, and Upwork contracts look correct
    • I have paid out Upwork contracts / manual NewDot requests
    • [BugZero Assignee] I have created a GH issue for creating/updating the regression test once above steps have been agreed upon
  15. Ollyws commented on Sep 14, 2026

    @Ollyws
    Contributor

    Not sure the checklist is required here as it was code cleanup but we can possibly add these tests:

    1. Open NewDot on staging and sign in.
    2. Verify the app reaches Home and the LHN renders, with no error banner, blank screen, or crash screen.
    3. Open any report and verify it renders fully — report header, message history, and composer.
    4. Navigate to the Account tab, verify the Profile page renders, then navigate back to Home and verify Home renders again.
    5. Reload the app (or fully background and reopen it on native) with the existing session. Verify it returns to Home without re-authenticating and the report still renders identically.
    6. Turn on Account > Troubleshoot > Force offline, verify the offline banner appears and Home plus the report still render, then turn it off and verify the app reconnects cleanly.
    7. Verify that no errors appear in the JS console.
    
  16. melvin-bot commented on Sep 14, 2026

    @melvin-bot

    @mallenexpensify Huh... This is 4 days overdue. Who can take care of this?

  17. mallenexpensify commented on Sep 14, 2026

    @mallenexpensify
    Contributor

    Claude posting on behalf of Matt:

    $125 to @Ollyws (Contributor+), 50%. One file, 17 changed lines, described by the reviewer himself as code cleanup.

    Contributor Nothing due, Melvin wrote the PR
    Contributor+ @Ollyws due $125 via NewDot, 50%
    Job price $250, standard, no price in title
    Reduction 50%, minor edit. 1 file, +14 / -3
    Increase None
    Blockers None linked
    Precedent 5 cases paid 50% for a small fix, ie. #96710, #94733. Also #95680 "tests weren't needed so $125"
    Needs a ruling No, but the size call is a judgment an employee may take differently
    How the amount is reached
    PR #99004, author MelvinBot
    Size 1 file, +14 / -3
    C+ response on this issue @Ollyws: "Not sure the checklist is required here as it was code cleanup"
    Reviewer checklist on the PR Posted by @Ollyws, all boxes checked
    Platform evidence Reviewer checklist 5 of 5 boxes checked, 0 of 5 with media, all four sections marked N/A
    • Job price $250. No price in the title.
    • Contributor: nothing due. Melvin wrote the PR.
    • Contributor+: 50%. One file and 17 changed lines is a fix of comparable size to the cases above. @Ollyws characterised it the same way.
    • No regression cut. No blocker linked. An Android Fabric crash surfaced mid-review and @Ollyws could not reproduce it; no regression issue was ever filed.
    • No increase.

    Two things I did not treat as reductions. The platform sections are all marked N/A, and there is no formal Contributor+ Checklist on this issue. Neither is a fault here: the change is a code-only cleanup with no UI, and a missing checklist is an absence rather than evidence the work was skipped.

    To pay the rules amount:

    ### Payment Summary
    Contributor+: @Ollyws due $125 via NewDot
    

    To pay full:

    ### Payment Summary
    Contributor+: @Ollyws due $250 via NewDot
    

    @Ollyws, flagging this since it proposes less than the standard price.


    Claude posting on behalf of Matt. PAYRUBRICTEST v2

  18. mallenexpensify commented on Sep 15, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @Ollyws due $125 via NewDot

  19. flaviadefaria commented on Sep 22, 2026

    @flaviadefaria
    Contributor

    Approved: @Ollyws due $125 via NewDot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentDailyExternalImprovement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions