Skip to content

Instantly share code, notes, and snippets.

@lucaswkuipers
Last active November 29, 2023 17:45
Show Gist options
  • Save lucaswkuipers/3356648a362b6c75dc28e0159f3197be to your computer and use it in GitHub Desktop.
Save lucaswkuipers/3356648a362b6c75dc28e0159f3197be to your computer and use it in GitHub Desktop.
View+onLoad
import SwiftUI
private struct OnLoad: ViewModifier {
let action: () -> Void
@State private var loaded = false
func body(content: Content) -> some View {
content.onAppear {
if !loaded {
loaded = true
action()
}
}
}
}
extension View {
func onLoad(perform action: @escaping () -> Void) -> some View {
modifier(OnLoad(action: action))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment