EnvironmentObject

Checking an existing code, I could see an environmentObject set in my main file. What does it mean ? it means I am creating an instance of my store and making them available for the entire application ? Like a static instance in other languages ? or there is something else ?

Thank you,

@main
struct myApp: App {
var myStore = MyStore()

var body: some Scene {
  WindowGroup {
    ContentView()
        .environmentObject(myStore)
Answered by ForumsContributor in
Accepted Answer

making them available for the entire application ?

Not for the entire application, but for all the subviews where environmentObject is given.

Like a static instance in other languages ?

It's a more structured way than using a static instance, more like using Dependency Injection. But how you describe it would depend on what you have experienced, you should better try and use it yourself.

EnvironmentObject
 
 
Q