Skip to content

Conversation

t0maboro
Copy link
Contributor

@t0maboro t0maboro commented Sep 17, 2025

Description

This change adds logic to detach the controller from its component when the component is destroyed. Unlike bottom tabs - where the Host handles detaching controllers from all its children - this approach delegates the responsibility to individual screens. This ensures a quicker response by detaching as soon as the screen is destroyed, rather than keeping views in the registry until the Host is unmounted.

Note: This is a temporary solution and will be replaced once a dedicated callback is available from Meta.

Closes: https://github.com/software-mansion/react-native-screens-labs/issues/127

Changes

  • Implemented RNSViewControllerInvalidating protocol for stack

Test code and steps to reproduce

Verified with logs that the callback is triggered and the controller's reference is removed

Checklist

  • Included code example that can be used to test this change
  • Ensured that CI passes

@t0maboro t0maboro marked this pull request as ready for review September 17, 2025 08:24
@t0maboro t0maboro force-pushed the @t0maboro/invalidate-new-stack branch from 2b07ead to 4d5bb10 Compare September 17, 2025 20:16
Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

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

I dislike the approach here a bit. I'll want to revisit the structure of the solution at some point. However, since the diff is compatible with what we currently have in other components let's keep it aligned for the moment being.

Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

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

Hey, but does it work reliably? What if you pop a screen through JS? Won't it be prematurely invalidated?

Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

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

no, wait. I've hurried with the approval.

Give me few minutes, I'll write another review

Comment on lines 145 to 153
- (void)mountingTransactionWillMount:(const facebook::react::MountingTransaction &)transaction
withSurfaceTelemetry:(const facebook::react::SurfaceTelemetry &)surfaceTelemetry
{
for (const auto &mutation : transaction.getMutations()) {
if ([self shouldInvalidateOnMutation:mutation]) {
[RNSViewControllerInvalidator invalidateViewIfDetached:self forRegistry:_invalidatedComponentsRegistry];
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

First thing is: why do we have to scan whole list of mutations in every screen component? Why can't we just do it once per transaction in host component?

Copy link
Contributor Author

@t0maboro t0maboro Sep 18, 2025

Choose a reason for hiding this comment

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

flushInvalidViews is triggered when the component is moved out of the window. In such a scenario, the Host will keep all Screens in its registry - we don't know for how long (if the Screen cannot invalidate itself immediately on the mutation, it needs to mark that the mutation came and invalidate on willMoveToWindow:nil)

Copy link
Member

Choose a reason for hiding this comment

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

🤔 I don't have good suggestion here unfortunately, but I dislike this solution for it's excessive (hopefully) transaction scanning. I'll try to come up with something better soon. We can discuss this on Monday.

@t0maboro
Copy link
Contributor Author

t0maboro commented Sep 18, 2025

Hey, but does it work reliably? What if you pop a screen through JS? Won't it be prematurely invalidated?

I was testing it on TestScreenStack for:

  • native back button
  • swipe to dismiss
  • 'pop' action

scenarios and all of them are determined by incoming Delete mutation so imo it works well as we have a guarantee that the component won't be mounted again.

@t0maboro t0maboro requested a review from kkafar September 18, 2025 10:21
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.

3 participants