Skip to content

How do I update a variable as a side effect of a state change in VSM? #46

Answered by albertbori
albertbori asked this question in Q&A
Discussion options

You must be logged in to vote

VSM state changes in SwiftUI are automatically handled when you declare your state variable with the @ViewState property wrapper and observe the output of your actions ($state.observe(model.load())). This means that whenever a new state is produced, it automatically calls the body property on your SwiftUI view and updates all subviews.

However, sometimes you need to synchronize a SwiftUI State, Binding, or StateObject variable with the VSM state. There are a few ways to do this:

Using onChange to Synchronize a State Variable

You can attach a closure to be called when the state changes, if the state is Equatable.

struct DemoView: View {
    @ViewState var state: DemoViewState = //...
    @S…

Replies: 1 comment

Comment options

albertbori
Jun 4, 2024
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by albertbori
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant