I have an @EnvironmentObject, store, I need to pass the variable as a parameter to my @StateObject's, vm, initializer. I first tried the View's init but an @EnvironmentObject is not yet existing upon View init. So then I tried the onAppear but onAppear does not allow use of self. Is this even possible? For reference I've included both the init and the onAppear, neither works.
@EnvironmentObject var store: Store
@StateObject private var vm: NewsViewModel
init() {
self._vm = StateObject(wrappedValue: NewsViewModel(self._store.wrappedValue.newsApi))
}
var body: some View {
if vm.show == true {
HStack {
Image("round-flag")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20)
.rotationEffect(.degrees(-28))
Text(self.vm.news?.subject ?? "")
.instaStylePrimary()
Button {
vm.show = false
} label: {
Image(systemName: "xmark")
.instaStylePrimary()
.frame(width: 12)
.foregroundColor(.gray)
}
}
}
}
}
3
0
2.7k