previewprovider environmentobject 1

previewprovider environmentobject

struct ItemDetail_Previews: PreviewProvider {
    static let order = Order()

    static var previews: some View {
        NavigationView {
            ItemDetail(item: MenuItem.example).environmentObject(order)

Here is what the above code is Doing:
1. We create a new struct called ItemDetail_Previews.
2. We create a static property called order that’s an Order object.
3. We create a static property called previews that’s a some View.
4. We create a NavigationView that contains an ItemDetail view.
5. We pass in the MenuItem.example as the item.
6. We pass in the order as the environment object.
7. We call the previewLayout() modifier and pass in .sizeThatFits to make the preview fit the screen.

Similar Posts