I'm trying to change a @State from its parent but nothing is changed in the view.
I have a simple CustomView with an enum that control its content:
struct CustomView: View {
enum ViewType: String {
case first, second, third
}
@State var viewType: ViewType
var body: some View {
content
}
@ViewBuilder private var content: some View {
switch viewType {
case .first:
Text("The first view")
case .second:
Text("The second view")
case .third:
Text("The last view")
}
}
}
And a superview with a button that tries to change its viewType value.
struct ContentView: View {
let customView = CustomView(viewType: .first)
var body: some View {
VStack(spacing: 40) {
customView
Button("Change") {
customView.viewType = .second
}
}
}
}
Maybe I shouldn't use @State but I had no success using Binding with enums.
Any idea?