I’m trying to observe some state variables, of which are set/loaded when the entire view appears using .onAppear()
. Problem is, this onChange
is firing when the user loads the view without editing the @State
variables due to my .onAppear
updating the state variables after some synchronous/asynchronous task.
struct ExampleView: View{
@State var ExampleVar: String = ""
var body: some View{
VStack{
TextField("Example", text: $ExampleVar)
}
.onChange(of: ExampleVar){
print("this will fire when the text field is updated within onAppear")
}
.onAppear(){
ExampleVar = "[email protected]"
}
}
}
I have tried setting a flag to conditionally execute code in the onChange
, however there seems to be some data race and the onChange
is executed when the view loads sometimes. Surely there is a easier way to disregard the first onChange
fire?