OnAppear method getting called when picker returns from selection

Viewed 86

In my swiftUI app, the onAppear method of my view is getting called when a picker I have in the same view returns from picking. I do not want my .onAppear method to be called except when the view loads. Is there an option or a good alternative to .onAppear that only gets called when the view loads? Thanks.

1 Answers

A possible workaround for this issue. The following code will only call the rest of the .onAppear method once, ensuring that it is only called the first time.

struct SampleView: View{
@State var alreadyDone = false

var body: some View{
Text("Content goes here")
.onAppear{
if !alreadyDone{
//Perform actions for view load
alreadyDone = true
}
}
}

}
Related