FirstView Appeared
is printed twice. Once when the view first loads and again when the NavigationLink is selected.
import SwiftUI
struct FirstView: View {
var body: some View {
NavigationView{
ZStack{
Text("FirstView").onAppear(perform: {print("FirstView Appeared")})
NavigationLink(destination: SecondView()) {
Text("Goto SecondView")
}.offset(y: 50)
}
}
}
}
struct SecondView: View {
var body: some View {
Text("SecondView").onAppear(perform: {print("SecondView Appeared")})
}
}
Running the code above in Xcode 12.0 beta on both the simulator and a personal device produce the output below when the NavigationLink is selected:
FirstView Appeared
FirstView Appeared
SecondView Appeared
Is this duplication of onAppear() expected behavior?
If so, what best practices are there to load some data when firstview
is created and then upon return to firstview
(since onAppear() would attempt to load some data when navigating away from firstView
)