the onAppear
onDisappear
modifiers might be known as a number of instances whereas the view is a part of the hierarchy.
I do know that there’s a trick to make an onLoad
ViewModifier like so
extension View {
func onLoad(carry out motion: (() -> Void)? = nil) -> some View {
self.modifier(ViewDidLoadModifier(motion: motion))
}
}
struct ViewDidLoadModifier: ViewModifier {
@State non-public var viewDidLoad = false
let motion: (() -> Void)?
func physique(content material: Content material) -> some View {
content material
.onAppear {
if viewDidLoad == false {
viewDidLoad = true
motion?()
}
}
}
}
From the above code, onLoad
will solely be known as as soon as
struct MyView: View {
var physique: some View {
Textual content("Hey View")
.onAppear {
// might print a number of instances
print("onAppear")
}
.onLoad {
// solely prints as soon as
// when the view first seems within the hierarchy
print("onLoad")
}
}
}
Is there a technique to have an onUnLoad
ViewModifier?
struct MyView: View {
var physique: some View {
Textual content("Hey View")
.onDisappear {
// might print a number of instances
print("onDisappear")
}
.onUnLoad {
// solely prints as soon as,
// when the view is totally eliminated
print("onUnLoad")
}
}
}