-
-
Save seifscape/f52d9d4d57564d81f8389e775392d299 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