Skip to content

Instantly share code, notes, and snippets.

@seifscape
Forked from lucaswkuipers/View+onLoad.swift
Created November 29, 2023 17:45
Show Gist options
  • Save seifscape/f52d9d4d57564d81f8389e775392d299 to your computer and use it in GitHub Desktop.
Save seifscape/f52d9d4d57564d81f8389e775392d299 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