🔸environment object
// app
struct MyApp: App {
// ⭐️ 1. declare & initialize a state object
@StateObject private var data = ModelData()
var body: some Scene {
WindowGroup {
RootView()
// ⭐️ 2. put an environment object.
.environmentObject(data)
}
}
}
// Subview: RootView's subview
struct Subview: View {
// ⭐️ get the environment object.
@EnvironmentObject var data: ModelData
var body: some View { ... }
}Last updated