Last active
February 24, 2024 18:34
-
-
Save hmlongco/56f7c1e9d092e51c1f3033209136fbc7 to your computer and use it in GitHub Desktop.
ID and State
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ParentView: View { | |
@State var id = UUID().uuidString | |
var body: some View { | |
VStack(spacing: 20) { | |
VStack { | |
Text("Parent View") | |
Text(id).font(.footnote) | |
} | |
ChildView(name: "Child Maintains State On Update") | |
ChildView(name: "Child Loses State On Update") | |
.id(id) | |
Button("Update") { | |
id = UUID().uuidString | |
} | |
} | |
} | |
} | |
struct ChildView: View { | |
let name: String | |
@State var id = UUID().uuidString | |
var body: some View { | |
VStack { | |
Text(name) | |
Text(id).font(.footnote) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment