SwiftUI_visionOS_Note

@EnvironmentObject

Apr 30, 2024
2 min read|

The @EnvironmentObject is a super-useful property wrapper that lets us put an observable object into the environment of our SwiftUI application.

By doing this, all of these views in the view hierarchy will then have access to this object.

This is the perfect solution for any case where you have a class that needs to be accessed by multiple views, but you don't want to manually bind it from view to view to view with initializers.

On the first view model, we have

@StateObject var viewModel: EnvironmentViewModel = EnvironmentViewModel()
swift

viewModel will use and change the data from EnvironmentViewModel.

On the second view, we have

ForEach(viewModel.dataArray, id: \.self) { item in
 NavigationLink(
	 destination: DetailView(selectedItem: item, viewModel: viewModel),
	label: {Text(item)})
swift

Means, in DetailView(selectedItem: item, viewModel: viewModel) string of selectedItem will use the data from viewMode.dataArray.

@ObservedObject var viewModel: EnvironmentViewModel
swift

While viewModel in second view gets observed, we clarify, viewModel here will be sharing viewModel from first view.

and it goes same for the Final View as well.

But what if we have subviews of subviews of subviews and goes on?

We use the viewModel on DetailView to just pass down the data to the FinalView.

 .environmentObject(viewModel)
swift

We set up the environmentObject that will pass around the viewModel

and when we want to use the viewModel we call it with @EnvironmentObject

@EnvironmentObject var viewModel: EnvironmentViewModel
swift

We can also remove the initializer

NavigationLink(destination: DetailView(selectedItem: item),
                                   label: {
                        Text(item)
                    }
swift

Everything on the hierarchy, will have an access to the data, instead of passing from the screen to screen.

Subscribe to our newsletter

Get the latest news and updates from our team