简体   繁体   中英

Failed to update SwiftUI preview when using .OnReceive Method

I have implemented the .OnReceive method in a SwiftUI view to be able to keep track of some variebles.

在创建

状态

But as you can see I have all the .onReceive's code is commented and the reason is that if I don't I get the following error on the canvas:

画布错误

This is my ContentView_Previews Struct: 在此处输入图片说明

Could anyone help me to understand the error? I suspect that the ContentView_Previews Struct need some extra value to be able to interpret the view on the canvas.

Thanks for the help!

This error means that the preview cannot render, and the reason for that is that it doesn't have the @EnvironmentObject var appState: States that your LoginView uses in the .onReceive .

So, you need to set it:

struct ContentView_Previews: PreviewProvider {
   static var previews: some View {
      LoginView()
         .previewLayout(.device)
         .environmentObject(States())  // here
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM