Skip to content

Instantly share code, notes, and snippets.

@623637646
Created August 19, 2023 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 623637646/ae5b6eebb1b197d38853b5ec709c699d to your computer and use it in GitHub Desktop.
Save 623637646/ae5b6eebb1b197d38853b5ec709c699d to your computer and use it in GitHub Desktop.
SwiftUI bug: Refresh logic of List is invalid in this case.
import SwiftUI
/// This demo shows a bug of SwiftUI. It's related to the refresh logic of "List"
///
/// The steps are:
/// 1. click "Go to next page"
/// 2. click "Click Me"
/// 3. Go back to home page
/// 4. The top view shows "Off", by right it should be "On".
///
/// If remove Section or replace List with ScrollView, the bug is gone.
///
/// I am using Xcode 14.3, Swift 5, Apple M2 Max
/// This bug can be reproduced on both iOS 16.6 (iPhone 8 plus) and Xcode Canvas
struct ContentView: View {
@State var isOn = false
@State var showItemInSection = false
var body: some View {
NavigationView {
List {
if isOn {
Text("On")
} else {
Text("Off")
}
NavigationLink(destination: NextView(callback: {
isOn = !isOn
showItemInSection = true
})){
Text("Go to next page")
}
Section(header: Text("A Section")) {
if showItemInSection {
Text("Name")
}
}
}.onAppear {
print("isOn: \(isOn)")
}
}
}
}
struct NextView: View {
var callback: ()-> Void
var body: some View {
Button("Click Me") {
callback()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment