Last active
November 29, 2023 17:45
-
-
Save lucaswkuipers/3356648a362b6c75dc28e0159f3197be to your computer and use it in GitHub Desktop.
View+onLoad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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