> 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

### **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](/files/-M64BqRbrGDnT7mkHVP3)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eneskaraosman53.gitbook.io/swiftui/state-and-data-flow/environmentobject.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
