i use @Observable
@Observable
class TextClass {
var text: String = ""
}
@State
struct ContentView: View {
@State var text: TextClass = .init()
var body: some View {
Form {
Text(text.text)
Button("upup") {
text.text += "1"
}
}
Form {
ChildView(text: text)
}
}
}
struct ChildView: View {
@State var text: TextClass
var body: some View {
Text (text.text)
Button("upup Child") {
text.text += "2"
}
}
}
@Binding
struct ContentView: View {
@State var text: TextClass = .init()
var body: some View {
Form {
Text(text.text)
Button("upup") {
text.text += "1"
}
}
Form {
ChildView(text: $text)
}
}
}
struct ChildView: View {
@Binding var text: TextClass
var body: some View {
Text (text.text)
Button("upup Child") {
text.text += "2"
}
}
}
@Bindable
struct ContentView: View {
@State var text: TextClass = .init()
var body: some View {
Form {
Text(text.text)
Button("upup") {
text.text += "1"
}
}
Form {
ChildView(text: text)
}
}
}
struct ChildView: View {
@Bindable var text: TextClass
var body: some View {
Text (text.text)
Button("upup Child") {
text.text += "2"
}
}
}
What are the differences between @State, @Binding, and @Bindable, all of which function similarly for bidirectional binding? Which one should I use?
I'm curious about the differences between the three.