Intentional Design or Technical Flaw? The Anomaly of onChange in SwiftUI Multi-Layer Navigation
Intentional Design or Technical Flaw? The Anomaly of onChange in SwiftUI Multi-Layer Navigation

Explore SwiftUI's `onChange` modifier “selective deafness” in multi-layer navigation. Understand why it might not trigger as expected and learn how to work around this issue.

SwiftUI provides the onChange modifier, allowing developers to listen for changes in specific values within a view and execute corresponding actions when those values change. Intuitively, as long as a view is part of the currently visible branch of the view tree (active), the corresponding closure should be triggered when the observed value changes. However, in certain navigation scenarios, the onChange modifier seems to become “selectively deaf,” inexplicably remaining silent even when the observed value changes. Is this a carefully designed feature by Apple, or a long-hidden code defect? This article aims to unveil this phenomenon and provide necessary caution to developers.