How to do didSet for State and Binding in SwiftUI
Published in
2 min readDec 17, 2020
Below is an example of a parent ContentView
with State
and a child Sidebar
with a Binding
.
The didSet
is only called for the property that is changed.
When we click Button in ContentView
, that changes State
property, so only the didSet
in ContentView
is called
When we click Button in Sidebar
, that changes Binding
property, so only the didSet
in Sidebar
is called
enum Tag: String {
case all
case settings
}struct ContentView: View {
@State
private var tag: Tag = .all {
didSet {
print("ContentView \(tag)")
}
} var body: some View {
Sidebar(tag: $tag)
Button(action: { tag = .settings }) {
Text("Button in ContentView")
}
}
}struct Sidebar: View {
@Binding
var tag: Tag {
didSet {
print("Sidebar \(tag)")
}
} var body: some View {
Text(tag.rawValue)
Button(action: { tag = .settings }) {
Text("Button in Sidebar")
}
}
}
Custom Binding with get set
Another way to observe Binding changes is to use custom Binding
with get, set
. Here even if we click Button in ContentView
, the set
block is triggered and here we can change State
var body: some View {
Sidebar(tag: Binding<Tag>(
get: {…