Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Last active February 24, 2024 18:34
Show Gist options
  • Save hmlongco/56f7c1e9d092e51c1f3033209136fbc7 to your computer and use it in GitHub Desktop.
Save hmlongco/56f7c1e9d092e51c1f3033209136fbc7 to your computer and use it in GitHub Desktop.
ID and State
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