// // MyApp.swift // import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @AppStorage("redBackground") private var redBackground: Bool = false var body: some View { ZStack { // Flipping "redBackground" will cause a reconstruction of the view hierarchy if redBackground { Color.red } else { Color.green } MyView(viewModel: MyViewModel()) } } } final class MyViewModel: ObservableObject { init() { print("MyViewModel.init") } } struct MyView: View { @StateObject var viewModel: MyViewModel @AppStorage("redBackground") private var redBackground: Bool = false // WARNING: Uncommenting this causes the view model to be recreated every reconstruction of the view! // init(viewModel: MyViewModel) { // self._viewModel = StateObject(wrappedValue: viewModel) // } var body: some View { VStack { Button("Toggle background") { redBackground = !redBackground } } } }