Skip to content

fix(patches): run the useInsertionEffect cleanup of a component removed inside a hidden Activity - #101577

Merged
roryabraham merged 4 commits into
Expensify:mainfrom
software-mansion-labs:dariusz-biela/fix/rn-hidden-subtree-insertion-effect-cleanup
Sep 25, 2026
Merged

roryabraham merged 4 commits into
Expensify:mainfrom
software-mansion-labs:dariusz-biela/fix/rn-hidden-subtree-insertion-effect-cleanup

Conversation

@dariusz-biela

@dariusz-biela dariusz-biela commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

When a component is removed while it is hidden, for example inside a hidden <Activity> or on a screen frozen by react-freeze, the native renderer skips the cleanup of its useInsertionEffect, so whatever that effect registered is never released. This PR backports the React fix that makes the cleanup run. React DOM on web already works this way, and React 19.3 does on every platform.

In the app

  • There is no visible change.

In the code

  • patches/react-native/react-native+0.86.0+044+run-insertion-effect-cleanup-in-hidden-subtree.patch removes the offscreenSubtreeWasHidden check in front of the insertion effect cleanup in commitDeletionEffectsOnFiber, in the development, production and profiling builds of the Fabric renderer. This check is controlled by the enableHiddenSubtreeInsertionEffectCleanup flag, which React turned on for React DOM (facebook/react#34372) and later removed (facebook/react#35918), but which is still off in the React Native 0.86 build.
  • patches/react-test-renderer/react-test-renderer+19.2.3+001+run-insertion-effect-cleanup-in-hidden-subtree.patch makes the same change in react-test-renderer, so that Jest tests behave like the app.
  • tests/unit/InsertionEffectCleanupInHiddenActivityTest.tsx checks that hiding a component does not run the cleanup, and that removing it runs the cleanup both when it is hidden and when it is visible.

Impact on the app

  • Only four insertion effects in the app have a cleanup: usePressability and the Animated props hook in React Native, and usePreventRemove and PreventRemoveProvider in React Navigation. Code in src/ does not use useInsertionEffect.
  • These cleanups only release what the removed component owns: press timers, animated nodes, and the screen's entry in the prevent-remove registry. None of them sets state or throws, so the change cannot cause new warnings or errors.
  • The cleanup now also runs when a screen frozen by react-freeze is removed together with the screen that covers it, which happens in the Reports and Workspaces split navigators and in tabs that use freezeOnBlur.

Fixed Issues

$ #98254
PROPOSAL: N/A

Tests

  1. Run npm install. Verify that patch-package applies react-native@0.86.0 (44 run-insertion-effect-cleanup-in-hidden-subtree) and react-test-renderer@19.2.3 (1 run-insertion-effect-cleanup-in-hidden-subtree) without warnings.
  2. Open node_modules/react-native/Libraries/Renderer/implementations/ReactFabric-dev.js and find the call to commitHookEffectListUnmount(Insertion, ...) in commitDeletionEffectsOnFiber. Verify that it is no longer preceded by offscreenSubtreeWasHidden ||, while the Layout call directly below it still is.
  3. On iOS and Android, run QA steps 1 to 3 below. Verify that every flow finishes without a red box, a crash or a new console error. The warnings navigation object hasn't been initialized, getSnapshot should be cached and state or screen passed in the params also appear without this patch, so they are not new.
  4. Run QA steps 1 to 3 on web. Verify that nothing changed there, because React DOM already runs this cleanup.
  • Verify that no errors appear in the JS console

Offline tests

N/A. This change only affects how React removes components, and it does not involve the network or Onyx.

QA Steps

Steps 2 and 3 remove a frozen screen together with the screen that covers it, which is the case this PR changes. Step 1 checks that buttons and animations, which use insertion effects, still work.

  1. On iOS or Android, tap the + button, then Create expense, then Manual. Enter an amount, a merchant and a category, and tap Create expense. Verify that the expense is created and appears on the screen that opens next. Depending on the account, this is either Spend or the workspace chat.
  2. Go to Inbox and open #admins or any other chat that has a thread. Tap "N Replies" under a message, and then tap Back twice. Verify that the thread opens, that the first Back returns to #admins, and that the second Back returns to the Inbox list.
  3. Go to Workspaces, tap New, and then tap Confirm. In the new workspace, open Members, tap Back, and open Overview. Then tap More, tap Delete, and confirm with Delete. Verify that the app returns to the Workspaces list and that the deleted workspace is no longer listed.
  4. Repeat steps 1 to 3 on web, and use the browser Back button wherever a step says Back. Verify the same results. On a wide window the Inbox list is always visible next to the chat, so in step 2 the second Back opens the previously visited page instead of the Inbox list.
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
android-1-create-expense.mp4
android-2-inbox-thread.mp4
android-3-delete-workspace.mp4
iOS: Native
ios-1-create-expense.mp4
ios-2-inbox-thread.mp4
ios-3-delete-workspace.mp4
MacOS: Chrome / Safari
web-1.mp4
web-2.mp4
web-3.mp4

…ed inside a hidden Activity

Fabric in react-native 0.86 (React 19.2.3 renderer, native-oss flags) skips the
insertion effect cleanup in commitDeletionEffectsOnFiber when the deleted fiber
sits in a hidden Offscreen subtree, because enableHiddenSubtreeInsertionEffectCleanup
stayed false in that fork. react-dom 19.2 runs it, and React 19.3 removed the
flag and runs it unconditionally (react/react#30954, Expensify#34372, Expensify#35918).

Patch 044 drops the guard in the dev, prod and profiling builds. react-test-renderer
19.2.3 gets the same patch so jest matches the runtime, and a test pins the three
paths: hide only, removal while hidden, removal while visible.
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR is possibly changing native code and/or updating libraries, it may cause problems with HybridApp. Please check if any patch updates are required in the HybridApp repo and run an AdHoc build to verify that HybridApp will not break. Ask Contributor Plus for help if you are not sure how to handle this. ⚠️

Comment thread patches/react-test-renderer/details.md Outdated
@dariusz-biela dariusz-biela changed the title [No QA] fix(patches): run the useInsertionEffect cleanup of a component removed inside a hidden Activity fix(patches): run the useInsertionEffect cleanup of a component removed inside a hidden Activity Sep 23, 2026
Comment thread tests/unit/InsertionEffectCleanupInHiddenActivityTest.tsx
@dariusz-biela
dariusz-biela marked this pull request as ready for review September 23, 2026 13:46
@dariusz-biela
dariusz-biela requested a review from a team as a code owner September 23, 2026 13:46
@melvin-bot
melvin-bot Bot requested review from truph01 and removed request for a team September 23, 2026 13:46
@melvin-bot

melvin-bot Bot commented Sep 23, 2026

Copy link
Copy Markdown

@truph01 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@truph01

truph01 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained """"why"""" the code was doing something instead of only explaining """"what"""" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Screen.Recording.2026-09-25.at.11.38.44.mov
Screen.Recording.2026-09-25.at.11.39.43.mov
Screen.Recording.2026-09-25.at.11.43.15.mov
Android: mWeb Chrome
Screen.Recording.2026-09-25.at.11.47.02.mov
iOS: HybridApp
Screen.Recording.2026-09-25.at.11.46.02.mov
iOS: mWeb Safari
Screen.Recording.2026-09-25.at.11.48.03.mov
MacOS: Chrome / Safari
Screen.Recording.2026-09-25.at.11.44.18.mov

@melvin-bot
melvin-bot Bot requested a review from roryabraham September 25, 2026 04:48

@roryabraham roryabraham left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it when we can add a patch for something already merged upstream, since we know they're temporary. Good fix!

@roryabraham
roryabraham merged commit 6c08347 into Expensify:main Sep 25, 2026
44 of 46 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚧 roryabraham has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/roryabraham in version: 9.4.94-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/Julesssss in version: 9.4.94-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants