Skip to content

Instantly share code, notes, and snippets.

@nteissler
Created January 2, 2026 00:53
Show Gist options
  • Select an option

  • Save nteissler/222d660d5a367d782ccbedc938fac2de to your computer and use it in GitHub Desktop.

Select an option

Save nteissler/222d660d5a367d782ccbedc938fac2de to your computer and use it in GitHub Desktop.
NavigationStack: Infinite Loops Investigation
/// https://mastodon.social/@davedelong/115815506537564728
/// Attempted reproduction for the behavior mentioned in the above toot.
import SwiftUI
struct ContentView: View {
@State private var path: [Int] = []
var body: some View {
NavigationStack(path: $path) {
VStack(spacing: 20) {
Text("NavigationStack Bug Demo")
.font(.largeTitle)
.padding()
Text("Current path: \(path.description)")
.font(.caption)
.foregroundColor(.secondary)
Divider()
Text("Instructions:")
.font(.headline)
Text("1. Navigate to Screen 1")
Text("2. Navigate to Screen 2")
Text("3. Navigate to Screen 1 again (duplicate in path)")
Text("4. Click the back button on macOS")
Text("→ This should cause an infinite loop on macOS (but I'm not observing this behavior?")
.foregroundColor(.red)
.padding(.top)
Divider()
ForEach(1..<4) { number in
Button("Navigate to Screen \(number)") {
path.append(number)
}
.buttonStyle(.borderedProminent)
}
if !path.isEmpty {
Button("Pop to Root") {
path.removeAll()
}
.buttonStyle(.bordered)
}
}
.padding()
.navigationDestination(for: Int.self) { number in
DetailView(number: number, path: $path)
}
.navigationTitle("Root")
}
}
}
struct DetailView: View {
let number: Int
@Binding var path: [Int]
var body: some View {
VStack(spacing: 20) {
Text("Screen \(number)")
.font(.largeTitle)
Text("Current path: \(path.description)")
.font(.caption)
.foregroundColor(.secondary)
Divider()
Text("Navigate to another screen to create a duplicate in the path:")
.multilineTextAlignment(.center)
.padding()
ForEach(1..<4) { num in
Button("Navigate to Screen \(num)") {
path.append(num)
}
.buttonStyle(.borderedProminent)
}
Button("Pop to Root") {
path.removeAll()
}
.buttonStyle(.bordered)
.padding(.top)
}
.padding()
.navigationTitle("Screen \(number)")
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment