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

Code Block 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")
    }
}


Why do you need StateObject instead of State ?

More explanation here:
https ://www.hackingwithswift. com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject

sample to reproduce the issue.

Sorry, but your code is not appropriate for a sample to reproduce the issue. Structs cannot be an @StateObject and your code does not compile.

You should better send a feedback to Apple attaching an appropriately made small sample.
Until Apple will fix the issue (I'm not sure if Apple would take this as a bug to be fixed), you may need to find some workaround.
Sorry the issue is a quick typing error, for a correction the ViewModel should be as follows

Code Block swift
final class ViewModel: ObservableObject {
init() {       
print("initialised")   
}
}


StateObject reinitialised when Form scrolls
 
 
Q