I've had this issue with many different custom views in SwiftUI, so why does code such as
struct HeaderText: View {
@State var text: String
@State var color: Color
var body: some View {
Text(text)
.font(.custom("Gilroy-SemiBold", size: 20))
.foregroundColor(color)
}
}
not update automatically when text or color changes. For instance, if I had a view such as
struct ContentView: View {
@State var text = "Hello"
var body: some View {
VStack {
HeaderText(text: self.text, color: .black)
Button("Change Text") {
self.text = "Goodbye"
}
}
}
}
I would have expected for the view to update with the new text, but it doesn't. I know you can accomplish this with @Binding, but I'm looking for a more elegant solution, similar to how Text works. I'm just using these views and structs as an example, so I'm aware these may not be best practices in general.
Thank you for the help!