-
-
Save laevandus/cd53c17945d2d6141323a2b93e7cf82d to your computer and use it in GitHub Desktop.
This file contains 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
struct PrepareViewData: ViewModifier { | |
@State var hasPrepared = false | |
let action: (() -> Void) | |
func body(content: Content) -> some View { | |
content | |
.onAppear { | |
if !hasPrepared { | |
action() | |
hasPrepared = true | |
} | |
} | |
} | |
} | |
extension View { | |
func prepare(perform action: @escaping () -> Void) -> some View { | |
modifier(PrepareViewData(action: action)) | |
} | |
} | |
struct ContentView: View { | |
@StateObject var viewModel = ViewModel() | |
var body: some View { | |
VStack { | |
// redacted | |
} | |
.prepare { | |
viewModel.prepare() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment