Hi, I just learned SwiftUI and am still confused on how using @EnvironmentObject correctly. should I declare @StateObject to use @EnvironmentObject on later views or not? Or maybe I can directly declare the used file in the .environmentObject property?
like this?:
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
NavigationView {
// Content
}
.environmentObject(viewModel)
}
}
or this?:
struct ContentView: View {
let viewModel = ViewModel()
var body: some View {
NavigationView {
// Content
}
.environmentObject(viewModel)
}
}
or maybe this?:
struct ContentView: View {
var body: some View {
NavigationView {
// Content
}
.environmentObject(ViewModel())
}
}
or these 3 actually works fine for different situations?
Selecting any option will automatically load the page