Post

Replies

Boosts

Views

Created

StateObject reinitialised when Form scrolls
I have a view inside of a Form. The view makes use of a@StateObject property. When I the view loads the state object property is initialised, when I scroll the view so it is off screen and back again the state object is initialised again. Is this expected behaviour? Below is small sample to reproduce the issue. I have not confirmed it but I suspect the behaviour is the same with a LazyVStack in a ScrollView swift struct MyForm: View {     var body: some View {         Form {             InnerView()             /*other form view content*/         }     } } struct InnerView: View {     @StateObject private var viewModel = ViewModel()     var body: some View {         Text("Hello")     } } struct ViewModel {     init() {         print("initialised")     } }
3
0
557
Mar ’21