> For the complete documentation index, see [llms.txt](https://eneskaraosman53.gitbook.io/swiftui/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eneskaraosman53.gitbook.io/swiftui/state-and-data-flow/environmentobject.md).

# EnvironmentObject

A linked View property that reads a \`ObservableObject\` supplied by an ancestor view that will automatically invalidate its view when the object changes.

### **State Management**

#### Share Data between Views using **@EnvironmentObject**

```swift
class User: ObservableObject {
    @Published var name = "Enes Karaosman"
}

// View 1
struct EditView: View {
    @EnvironmentObject var user: User
    
    var body: some View {
        TextField("Name", text: $user.name)
    }
}

// View 2
struct DisplayView: View {
    @EnvironmentObject var user: User
    
    var body: some View {
        Text(user.name)
    }
}

// Container View to demonstrate communication
struct DemoView: View {
    let user = User()
    
    var body: some View {
        VStack {
            EditView()//.environmentObject(user)
            DisplayView()//.environmentObject(user)
        }
        // Since both views wrapped by same container
        // We can bind here as well.
        .environmentObject(user)
    }
}
```

{% hint style="info" %}
EnvironmentObject does not require to be passed explicitly down to child views.
{% endhint %}

```swift
// Lets demonstrate the information above

// Dummy class for testing EnvironmentObject
class EnvTest: ObservableObject {
    @Published var testStr = "inital"
}

// In SceneDelegate, add those
let envTest = EnvTest()
let contentView = ContentView() // Assume ContentView is initial View
    .environmentObject(envTest)
    
struct ContentView: View {

    // Now we are able to read environmentObject 
    // passed from SceneDelegate, could be anywhere
    @EnvironmentObject() var envTest: EnvTest
    
    var body: some View {
        VStack(spacing: 16) {
            
            Text("Main")
            TextField("Placeholder", text: $envTest.testStr)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            
            Rectangle().foregroundColor(.red).frame(height: 2)
            Child1() // Dummy Child View 
                
        }.padding()
    }
    
}
```

Lets also create 2 dummy child view.

```swift
struct Child1: View {
    
    @EnvironmentObject var envTest: EnvTest
    
    var body: some View {
        VStack {
            Text("Child 1").font(.headline)
            Text(envTest.testStr)
            Rectangle().foregroundColor(.blue).frame(height: 2)
            Child2()
        }
    }
    
}


struct Child2: View {
    
    @EnvironmentObject() var envTest: EnvTest
    
    var body: some View {
        VStack {
            Text("Child 2").font(.headline)
            TextField("Placeholder", text: $envTest.testStr)
                .textFieldStyle(RoundedBorderTextFieldStyle())
        }
    }
    
}
```

Now when we edit envTest (`EnvironmentObject`) in any child, the changes reflected everywhere it is used.

![EnvironmentObject across child views](https://1761232408-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M5Upqg27mOVmaED1CFL%2F-M64AU84znLL1AMFGah6%2F-M64BqRbrGDnT7mkHVP3%2FEnvironmentObjectAcrossChildViews.gif?alt=media\&token=f0428583-1f9f-4c4f-9927-d91f45b98d9c)
